代码之家  ›  专栏  ›  技术社区  ›  Toran Billups

jQuery为每个节点附加子节点

  •  5
  • Toran Billups  · 技术社区  · 16 年前

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

    $(this).children().each(    
        $(div).appendChild(this.childNodes.length - 1);
    );
    
    2 回复  |  直到 16 年前
        1
  •  8
  •   Adam Bellaire    16 年前

    each() this children() 原来的jQuery对象。

    因此:

    $(this).children().each(function() {    
        $(div).appendChild($(this));
    });
    
        2
  •  0
  •   RaYell    16 年前

    each 呼叫:

    $(this).children().each(function() {
        $(div).appendChild(this.childNodes.length - 1);
    });
    

    function doSomething() {
        $(div).appendChild(this.childNodes.length - 1);
    }
    
    $(this).children().each(doSomething);
    

    我不确定你的代码是否无法改进,但当我只看到其中的一小部分时,我几乎无话可说。