jQuery 是否在 appendTo / prependTo 中使用克隆

Does jQuery use clone in appendTo / prependTo

大家好,我正在为我的网站编写 jquery(不是全部)的一个小版本。(为了缩短我的代码)

我到达了 prependTo 和 appendTo 方法,这对我提出了一个问题:

Does jQuery use clone method or cloneNode method to build this functions?


谢谢....

根据 appentTo() docs,这取决于您定位的是一个现有元素还是多个元素。

We can also select an element on the page and insert it into another:

$( "h2" ).appendTo( $( ".container" ) );

If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned) and a new set consisting of the inserted element is returned:

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
  <h2>Greetings</h2>
</div>

If there is more than one target element, however, cloned copies of the inserted element will be created for each target except the last, and that new set (the original element plus clones) is returned.

根据下面的@Andreas 评论,如果满足正确的条件,domManip 方法似乎可以克隆节点。 https://github.com/jquery/jquery/blob/master/src/manipulation.js#L310

    append: function() {
        return domManip( this, arguments, function( elem ) {
            if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
                var target = manipulationTarget( this, elem );
                target.appendChild( elem );
            }
        } );
    },