简体   繁体   English

如何使用jQuery限制多个元素的字符串长度?

[英]How to limit string length of multiple elements with jquery?

I want to limit the string length of all my $("a.paragraph"). 我想限制所有$(“ a.paragraph”)的字符串长度。 I have the following code: 我有以下代码:

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);
}

This code reworks the first element only and displays the result on all paragraphs. 此代码仅重做第一个元素,并在所有段落上显示结果。 How can I loop every single paragraph? 如何循环每个段落?

You can make use of jQuery's each function : 您可以利用jQuery的each函数

$('a.paragraph').each(function() {
    var paragraph = $(this).text();
    // ... do stuff here
})

Using .each : 使用.each

$('a.paragraph').each(function() {
    var paragraph = $(this).text();
    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
        $(this).text(introduction);
    }
});

You need to find each paragraph and loop around them: 您需要找到每个段落并在它们周围循环:

$("a.paragraph").each(function() {
    var paragraph = $(this).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);
    }
});

You need to loop over every element. 您需要遍历每个元素。 The behavior you are experiencing is the way jQuery works by default. 您遇到的行为是jQuery默认工作的方式。

$("a.paragraph").each(function(i,e) {
    var paragraph = $(e).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
        $(e).text(introduction);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM