Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery限制多个元素的字符串长度?_Javascript_Jquery_Loops - Fatal编程技术网

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

Javascript 如何使用jquery限制多个元素的字符串长度?,javascript,jquery,loops,Javascript,Jquery,Loops,我想限制我所有$(“a.paragration”)的字符串长度。我有以下代码: var paragraph = $("a.paragraph").text(); var maxlength = 500; var strlength = paragraph.length; if (strlength > maxlength) { var introduction = paragraph.substr(0,maxlength); // cut string var sea

我想限制我所有$(“a.paragration”)的字符串长度。我有以下代码:

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);
}
此代码仅重写第一个元素,并在所有段落上显示结果。如何循环每个段落?

您可以利用:

使用:


您需要找到每个段落并围绕它们循环:

$("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);
    }
});

您需要在每个元素上循环。您所经历的行为是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);
    }
});

谢谢,这正是我想要的。我试了每一个,但忘了用“这个”。
$("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);
    }
});
$("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);
    }
});