作家
登录

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

作者: 来源:www.28hudong.com 2013-03-30 00:15:34 阅读 我要评论

复制代码 代码如下: <nav class="cf_nav clearfix"> <ul> <li> <a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com" title="首页"> <div class="nav_block"> <span>首页</span> <span class="hover">首页</span> </div> </a> </li> <li> <a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/submit" title="投稿"> <div class="nav_block"> <span>投稿</span> <span class="hover">投稿</span> </div> </a> </li> <li> <a href="http://lizhuangs.diandian.com/inbox " title="私信 "> <div class="nav_block"> <span>私信 </span> <span class="hover">私信 </span> </div> </a> </li> <li> <a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/archive" title="存档"> <div class="nav_block"> <span>存档</span> <span class="hover">存档</span> </div> </a> </li> <li> <a href="http://lizhuangs.18021051d350c3871c0a.tpl.diandian.com/rss" title="订阅"> <div class="nav_block"> <span>订阅</span> <span class="hover">订阅</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);

  推荐阅读

  ie支持function.bind()方法实现代码

前端开发者应该很清楚 Javscript 脚本的 function 函数对象可以通过 call 或 apply 方法,使其改变内部作用域(this)所指向的对象,实现更多可扩展的功能开发。ie 原生支持 function 对象的 call 和 apply 方法,在>>>详细阅读


本文标题:CSS(js)限制页面显示的文本字符长度

地址:http://www.17bianji.com/kaifa2/JS/22664.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)