-1

这就是问题所在:我有一个用链接填充列表的函数,但是 $.each 函数通过 linkList 对象进行两个循环并填充容器两次(2 * 2 = 4 个项目而不是 2)有人可以解释为什么吗?

这是链接列表:

var linkList = {
    link1:["Title","Source","http://google.com","file.pdf"],
    link2:["Title2","Source2","http://google.com","file.pdf"]
};

这是功能:

function injectLinks(){

    $.each(linkList, function(i,item) {

        var title = item[0];
        var source = item[2];
        var extern = item[3];


        $('#linkListView').append('\
                <li>\
                    <a rel="external" href="'+extern+'">\
                            <h3>'+title+'</h3><p>'+source+'</p>\
                    </a>\
                </li>\
            ');
        });



}
4

2 回答 2

0

Found my problem, actually the populating function was called inside of a $document.ready function which was called once DOM is ready and another time after the populating function finished its work (DOM manipulation calls the document ready function) that's why I had double population. Thanks for your help.

So I used this technique:

var pageInitialized = false;

$(function(){
    if(pageInitialized) return;
    pageInitialized = true;
    //do things
}
于 2012-11-23T22:05:46.533 回答
0

我刚刚浏览了您的代码,发现实际上它循环了两次,并且它仅用两个项目填充容器,但是由于您<h3><li>.

这是jsFiddle修改后的代码。

于 2012-11-22T19:41:42.723 回答