简体   繁体   English

jQuery each() 循环的迭代次数超出预期

[英]jQuery each() loop iterating more than expected

In the below code, I take a div, clone it for as many items within the new javascript object, populate each clone with a title and post, and then I clear the first parent.在下面的代码中,我使用一个 div,为新的 javascript 对象中的尽可能多的项目克隆它,用标题和帖子填充每个克隆,然后清除第一个父级。 console.log(message.length) after parsed returns 4. So I expect the loop to iterate 4 times, leaving 4 divs of title and post.解析后的console.log(message.length)返回 4。所以我希望循环迭代 4 次,留下 4 个标题和帖子的 div。 However, this code returns 1 div of "first note" and "test message...", 2 divs of "second test" and "some more...", 4 divs of "third test" and "some more...", and 8 divs of "fourth test" and "even more...".但是,此代码返回“第一个注释”和“测试消息...”的 1 个 div、“第二个测试”和“更多...”的 2 个 div、“第三个测试”和“更多...”的 4 个 div。 .”,以及“第四次测试”和“更多……”的 8 个 div。 What am I missing?我错过了什么?

This is the what is stored in the message object这是消息对象中存储的内容

message = [{"date": "Sat, 15 Oct 2016 14:47:00 GMT", "id": 5, "post": "test message for the body of the post", "title": "first note"},
{"date": "Sat, 15 Oct 2016 15:20:06 GMT", "id": 6, "post": "some more text for a new test post", "title": "second test"},
{"date": "Mon, 17 Oct 2016 13:24:05 GMT", "id": 7, "post": "some more text", "title": "third test"},
{"date": "Mon, 17 Oct 2016 13:28:09 GMT", "id": 8, "post": "even more text to add", "title": "fourth test"}]

This is from userhome.js这是来自 userhome.js

        else {
            var noteObj = JSON.parse(message);
            var note = null;

            $.each(noteObj, function (index, value) {
                note = $(".list-group").clone();
                $(note).find("h4").text(value.title);
                $(note).find("p").text(value.post);
                $(".jumbotron").append(note);
            });

            $(".list-group:first").empty();

            //print success message to console
            console.log("successfully retrieved notes");
        }

This is from userhome.html这是来自 userhome.html

<div class="jumbotron">
        <h1>welcome!</h1>
        <div class="list-group">
            <a href="#" class="list-group-item active">
                <!--start dynamic section-->
                    <h4 class="list-group-item-heading"></h4>
                    <p class="list-group-item-text"></p>
                <!--end dynamic section-->
            </a>
        </div>
    </div>

When you call $(".list-group").clone() , you are cloning the entire list on each iteration.当您调用$(".list-group").clone() ,您将在每次迭代中克隆整个列表。 Therefore, you get 1 clone on the first iteration, 2 clones on the second iteration, 4 clones on the third iteration, etc. You then change the title and post of all of the clones, and then add all of the clones to jumbotron.因此,您在第一次迭代中获得 1 个克隆,在第二次迭代中获得 2 个克隆,在第三次迭代中获得 4 个克隆,依此类推。然后更改所有克隆的标题和帖子,然后将所有克隆添加到 jumbotron。 So be more specific with your selection.因此,请更具体地选择您的选择。 For example, you could use $(".list-group").first().clone() .例如,您可以使用$(".list-group").first().clone()

Each loop are cloning the whole list.每个循环都克隆整个列表。 That's why you get, 1, 2, 4, 8...这就是为什么你会得到 1, 2, 4, 8 ...

You only want to clone the first item of the list.您只想克隆列表的第一项。

On your code change this line:在您的代码中更改此行:

note = $(".list-group").clone();

to:到:

note = $(".list-group").first().clone();

That will fix it.那将修复它。

Also you don't need to use $(note) each time you call the note , because note is already a $jQuery element itself.此外,每次调用note时都不需要使用$(note) ,因为note本身已经是一个 $jQuery 元素。 You can have just this:你可以有这个:

note.find("h4").text(value.title);
note.find("p").text(value.post);

Also is best practice to use the $ sign when you are refering to a jQuery element, so your final code would be like this:在引用 jQuery 元素时使用 $ 符号也是最佳实践,因此您的最终代码将如下所示:

    else {
        var noteObj = JSON.parse(message);
        var $note = null;

        $.each(noteObj, function (index, value) {
            $note = $(".list-group").clone();
            $note.find("h4").text(value.title);
            $note.find("p").text(value.post);
            $(".jumbotron").append(note);
        });

        $(".list-group:first").empty();

        //print success message to console
        console.log("successfully retrieved notes");
    }

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

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