简体   繁体   English

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

[英]Does jQuery use clone in appendTo / prependTo

Hello guys I'm writing a little version of jquery(not all of that) for my website.(for shortnize my code)大家好,我正在为我的网站编写一个小版本的 jquery(不是全部)。(用于缩短我的代码)

i reached to prependTo and appendTo methods which raise a question for me:我达到了 prependTo 和 appendTo 方法,这对我提出了一个问题:

Does jQuery use clone method or cloneNode method to build this functions? jQuery 是否使用克隆方法或克隆节点方法来构建此功能?


Thanks....谢谢....

According to the appentTo() docs , it depends if you're targeting one existing element or more than one.根据appentTo()文档,这取决于您是针对一个现有元素还是多个现有元素。

We can also select an element on the page and insert it into another:我们还可以 select 页面上的一个元素并将其插入另一个元素:

$( "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:如果以这种方式选择的元素插入到 DOM 中其他位置的单个位置,它将被移动到目标中(未克隆)并返回由插入元素组成的新集合:

<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.但是,如果有多个目标元素,则将为除最后一个之外的每个目标创建插入元素的克隆副本,并返回新集合(原始元素加上克隆)。

As per @Andreas comment below, it appears as though the domManip method can clone nodes if the correct conditions are met.根据下面的@Andreas 评论,如果满足正确的条件,似乎domManip方法可以克隆节点。 https://github.com/jquery/jquery/blob/master/src/manipulation.js#L310 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 );
            }
        } );
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM