jQuery为每个节点添加子节点

Tor*_*ups 5 foreach jquery

在下面的代码中,我试图遍历每个子节点并将子节点附加到另一个元素 - 循环内的正确语法是什么?

$(this).children().each(    
    $(div).appendChild(this.childNodes.length - 1);
);
Run Code Online (Sandbox Code Playgroud)

Ada*_*ire 8

each()函数中,this指的是你正在迭代的东西,在这种情况下是children().它不是this原始jQuery对象的.

因此:

$(this).children().each(function() {    
    $(div).appendChild($(this));
});
Run Code Online (Sandbox Code Playgroud)