您好,欢迎访问一九零五行业门户网

CSS(js)限制页面显示的文本字符长度_javascript技巧

复制代码 代码如下:
首页
首页
投稿
投稿
私信
私信
存档
存档
订阅
订阅
如果我在限制上面的span中的字符。
$(nav_block span).wordlimit(8);
它使用了下面的一个算定义jquery插件的方法,这是我从点点网扣下来的:
复制代码 代码如下:
// copyright c by zhangxinxu v1.0 2009-09-05
// http://www.zhangxinxu.com
/* $(.test1).wordlimit(); 自动获取css宽度进行处理,如果css中未对.test1给定宽度,则不起作用
$(.test2).wordlimit(24); 截取字符数,值为大于0的整数,这里表示class为test2的标签内字符数最多24个
*/
(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);
其它类似信息

推荐信息