链接文本的substr方法并添加省略号?

mat*_*att 2 jquery substr

$("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?

sje*_*397 8

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)