您可以用jQuery做到这一点: -
your text goes here. Try long text here....
a.morelink {
text-decoration: none;
outline: none;
}
.morecontent span {
display: none;
}
.comment {
width: 400px;
background-color: #f0f0f0;
margin: 10px;
}
var showChar = 120;
var ellipsestext = "...";
var moretext = "more";
var lesstext = "less";
$(".columns").each(function() {
var content = $(this).html();
if (content.length > showChar) {
var first = content.substr(0, showChar);
var second = content.substr(showChar - 1, content.length - showChar);
var html = first + '' + ellipsestext+ ' ' + second + ' ' + moretext + '';
$(this).html(html);
}
});
$(".morelink").click(function(){
if($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
} else {
$(this).addClass("less");
$(this).html(lesstext);
}
$(this).parent().prev().toggle();
$(this).prev().toggle();
return false;
});