有一些文字由165个字符组成.我只需显示前155个字符,但155个字符中的最后5个字符应替换为“…..”(elipses),其余字符应删除.
fiddle
<head> <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(function(){ var txt= $('div').text(); for(i=0;i<2;i++){ alert(txt.charat(150+ i)) } }) </script> </head> <body> <div style="width:100px"> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that </div> </body>
解决方法
查看
substring function here,并发表声明:
var txt= $('#restrict').text(); if(txt.length > 155) $('#result').text(txt.substring(0,150) + '.....');