关于jquery appendTo方法 是移动对象还是复制对象

结论:appendto 是将 匹配的元素 移动到 目标元素

在 jquery 官方网站中 发现:

1
      
      
$( "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:

1
2
3
4
5
      
      
<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 after the first, and that new set (the original element plus clones) is returned.

按照我的理解,如果target目标元素只有1个,那么是将需要移动的元素移动至target目标元素,移除这些需要移动的元素的原始位置;

如果是多个target目标元素,那么是将需要移动的元素的copy移动至target目标元素,而不移除这些需要移动的元素的原有位置。


但尝试后发现:无论是多个还是单个 都是 移动。。不是复制。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>appendTo demo</title>
  <style>
  .foo {
    background: yellow;
  }
  </style>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<span>I have nothing more to say1... </span>
 <div class="foo">FOO! </div>
 
<script>
alert(($( "span" ).appendTo( ".foo" )).length);//alert  1
//$( "span" ).appendTo( ".foo" );
</script>
 
</body>
</html>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>appendTo demo</title>
  <style>
  .foo {
    background: yellow;
  }
  </style>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 <span>I have nothing more to say1... </span>

<span>I have nothing more to say1... </span>
 <div class="foo">FOO! </div>
  <div class="foo">FOO! </div>
 
<script>
alert(($( "span" ).appendTo( ".foo" )).length); //alert 4 
//$( "span" ).appendTo( ".foo" );
</script>
 
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值