我的div里有一个很长的文字。我希望它只显示十个字符,然后其余内容被...
替换。
<div id="name">wahahahahahahahahahahaha</div>
在Jquery
$("div#name").text(function(index, currentText) {
return currentText.substr(0, 10);
});
这个jQuery工作,长文本停止到第十个。
答案 0 :(得分:3)
添加条件以检查文本是否超过10个字符。
$("div#name").text(function(index, currentText) {
var newText = currentText.substr(0, 10);
if(currentText.length > 10)
newText += "...";
return newText ;
});