append on foreach jquery中的元素



如何执行此操作:

$(".results tr:not(:first)").each(function(e){
    // somme code
    e.append("<td> display value </td>")
});

当然,这不起作用,我试着用第二个值(f)en也不起作用我的全部代码是:

function affStat(){
    if(localStorage.table == "") return ;
    var table = JSON.parse(localStorage.table);
    $(".results tr:not(:first)").each(function(e, f){
        try{
            //console.log(f.children[1].children[0].title);
            var titre = f.children[1].children[0].title ;
            //console.log(f.children[5].textContent);
            var taille = f.children[5].textContent ;
            var hash = btoa(titre + taille);
            if($.inArray(hash, table)){
             // XXXXXXXXXXXXXXX.append("<td> V </td>");
                console.log(hash + "   V");
            }else{
             // XXXXXXXXXXXXXXXXX.append("<td> X </td>");
                console.log(hash + "   X");
            }
        }catch(err){
            console.log(err);
        }
    })
}

在您的例子中,e指的是元素的索引。

.each()的语法是

.each( function(index, Element) )

将您的代码更改为,这里我使用了this

$(".results tr:not(:first)").each(function(index, element){
    // somme code
    $(this).append("<td> display value </td>")
});

更改为:

$(this).append("<td> display value </td>");
//^^^^^----$(this) refers here to the current tr in loop

您的代码问题:

您已将e用于当前元素,该元素代表该trindex
这里是.each( function(e)),如果你在函数中只传递一个自变量,那么它会查找索引,因为每个文档的第一个自变量是index:

.each( function(index, Element))

所以我认为你也可以尝试更改为:

$(".results tr:not(:first)").each(function(index, e){ // <----pass another arg
   // somme code
   e.append("<td> display value </td>")
});

最新更新