jquery 控制 clone( )

clone( )

克隆匹配DOM,并选中当前克隆匹配元素集合

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("b").clone().prependTo("p");
  });
  </script>
  
</head>
<body>
  <b>Hello</b><p>, how are you?</p>
</body>

 

$("b").clone().prependTo("p");

将匹配b元素集合克隆,并添加在匹配P元素之前。

 

 

clone( true )

克隆匹配DOM,并获取其事件句柄,选中当前克隆匹配元素集合

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("button").click(function(){
      $(this).clone(true).insertAfter(this);
    });

  });
  </script>
  
</head>
<body>
  <button>Clone Me!</button>
</body>
</html>

 

 $(this).clone(true).insertAfter(this);
克隆当前按钮并获取click事件句柄,复制至该按钮后面

你可能感兴趣的:(html,jquery)