clone()與clone(true)同為克隆
clone()表示復制標簽本身,
clone(true)會將標簽綁定的事件一起復制
來看案例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> .c1 { background-color: red; } </style> <body> <input type="button" class="c1" value="點我復制"> <script> var $bEle = $(".c1"); $bEle.on("click",function () { $bEle.clone(true).insertAfter(this) //或者 $bEle.clone(this).insertAfter(this) }) </script> </body> </html>
顯示效果
這三個按鈕都可以點擊並實現復制以此往后插入。
如果JS部分代碼該為這樣:
<script> var $bEle = $(".c1"); $bEle.on("click",function () { $bEle.clone().insertAfter(this) }) </script>
那么僅僅只有第一個按鈕可以通過點擊實現自我復制,后面克隆得到的其他按鈕都沒有復制事件