$("a.newslinks").each(function(){
if ($(this).text().length > 38) {
$(this).text().substr(35); //does not work
$(this).append('...'); //works
$(this).css({ "color" : "#ff00cc" }); //works
}
});
Run Code Online (Sandbox Code Playgroud)
如果链接的文本长度超过38个字符,如何将其修剪为35个字符并在末尾添加一个elipses?
substr(35)将从字符串的开头处删除 35个字符 - 不要将其限制为35个字符长度.
尝试:
.substr(0, 35)
Run Code Online (Sandbox Code Playgroud)
此外,此函数只返回一个新字符串 - 它不会更改原始字符串.所以你需要这样做
$(this).text($(this).text().substr(0, 35));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11523 次 |
| 最近记录: |