我想限制所有$(「a.paragraph」)的字符串長度。我有以下代碼:如何用jquery限制多個元素的字符串長度?
var paragraph = $("a.paragraph").text();
var maxlength = 500;
var strlength = paragraph.length;
if (strlength > maxlength) {
var introduction = paragraph.substr(0,maxlength); // cut string
var search = introduction.lastIndexOf(" "); // find position of last space (last word cannot be cut)
introduction = introduction.substr(0, search); // cut string until last space
introduction = introduction + "..."; // add ... in the end
$("a.paragraph").text(introduction);
}
此代碼僅返工的第一個元素,並顯示所有段落的結果。我怎樣才能循環每一段?
謝謝你,這正是我所尋找的。我嘗試了每個,但忘了使用'這個'。 – 2012-04-02 18:58:32