有一些文本由 165 个字符组成。我只需要显示前 155 个字符,但 155 个字符中的最后 5 个字符应替换为“.....”(省略号),其余字符应删除。 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) + '.....');
关于jquery - 使用 jquery 设置文本限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840492/