---
title: "jquery 控制 clone( ) "
author: "KAXU"
date: "2009-02-03T17:10:11Z"
tags: ["HTML", "jQuery"]
slug: "jquery-clone"
draft: false
lastmod: "2009-02-03T17:17:24Z"
id: 322556
url: "https://kaxu.iteye.com/blog/322556"
---

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事件句柄，复制至该按钮后面