做进做了一个项目 多行文字 字数多了 添加省略号 百度上 一看 千篇一律 后来 还是 发现 张鑫旭博客 写的 拥有自己的见解
(function($){
$.fn.wordLimit = function(num){
this.each(function(){
if(!num){
var copyThis = $(this.cloneNode(true)).hide().css({
'position': 'absolute',
'width': 'auto',
'overflow': 'visible'
});
$(this).after(copyThis);
if(copyThis.width()>$(this).width()){
$(this).text($(this).text().substring(0,$(this).text().length-4));
$(this).html($(this).html()+'...');
copyThis.remove();
$(this).wordLimit();
}else{
copyThis.remove();
return;
}
}else{
var maxwidth=num;
if($(this).text().length>maxwidth){
$(this).text($(this).text().substring(0,maxwidth));
$(this).html($(this).html()+'...');
}
}
});
}
})(jQuery);
采用了 jq 扩展 很好用
$(".ellipsisTestBig").wordLimit(130);
130 为行的字符数 超过这个数值 就显示省略号...了 、 非常感谢 张鑫旭