手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >CSS(js)限制页面显示的文本字符长度
CSS(js)限制页面显示的文本字符长度
摘要:复制代码代码如下:首页首页投稿投稿私信私信存档存档订阅订阅如果我在限制上面的span中的字符。$("nav_blockspan").word...

复制代码 代码如下:

<nav>

<ul>

<li>

<a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com" title="首页">

<div>

<span>首页</span>

<span>首页</span>

</div>

</a>

</li>

<li>

<a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/submit" title="投稿">

<div>

<span>投稿</span>

<span>投稿</span>

</div>

</a>

</li>

<li>

<a href="http://lizhuangs.diandian.com/inbox " title="私信 ">

<div>

<span>私信 </span>

<span>私信 </span>

</div>

</a>

</li>

<li>

<a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/archive" title="存档">

<div>

<span>存档</span>

<span>存档</span>

</div>

</a>

</li>

<li>

<a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/rss" title="订阅">

<div>

<span>订阅</span>

<span>订阅</span>

</div>

</a>

</li>

如果我在限制上面的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);

【CSS(js)限制页面显示的文本字符长度】相关文章:

jQuery实现限制textarea文本框输入字符数量的方法

Node.js重新刷新session过期时间的方法

js禁止页面刷新与后退的方法

jQuery实现自动滚动到页面顶端的方法

js去除浏览器默认底图的方法

Mozilla中显示textarea中选择的文字

javascript常用的方法分享

javascript制作的滑动图片菜单

JS动态增删表格行的方法

jquery预加载图片的方法

精品推荐
分类导航