2010-09-17 65 views
2
$("a.newslinks").each(function(){ 
     if ($(this).text().length > 38) { 
      $(this).text().substr(35); //does not work 
      $(this).append('...'); //works 
      $(this).css({ "color" : "#ff00cc" }); //works 
     } 
    }); 

如果一个链接的文本长度超过38个字符,我如何将它修剪为35个字符,并在末尾添加一个缩略图?substr方法链接文本并添加省略号?

回答

8

substr(35) will chop 35个字符在字符串开头 - 不限制为35个字符的长度。

尝试:

.substr(0, 35) 

此外,该功能只返回一个新字符串 - 它不改变原。所以,你需要做的

$(this).text($(this).text().substr(0, 35)); 
2

尝试:

$(this).text($(this).text().substr(0, 35));