代码之家  ›  专栏  ›  技术社区  ›  matt

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

  •  2
  • matt  · 技术社区  · 14 年前
    $("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个字符,并在末尾添加省略号?

    2 回复  |  直到 12 年前
        1
  •  8
  •   sje397    14 年前

    substr(35)

    尝试:

    .substr(0, 35)
    

    $(this).text($(this).text().substr(0, 35)); 
    
        2
  •  2
  •   Kelsey    14 年前

    尝试:

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