jQuery中clone和clone(true)的区别
今天要写的是clone和clone(true)的区别
两者长得很像呀,clone(true)比clone() 多了一个true。看下图白白的牙,笑起来就是这么灿烂。有了true就跟笑起来一样,有了笑这个行为事件。

那么 true这个有什么效果么?
语言描述就是 复制的节点具有行为,比如click啦。
clone(true) 方法 是复制一个元素及其所有事件,
clone() 方法 是复制一个元素,不包含其所有事件。
用下例子描述一下
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <script src="jquery-1.11.1.js"></script> //导入jquery-1.11.1.js
6 <script>
8 function clone_btn(e){
9 var newbtn=$("#test").clone(true);//如果把true去掉,新复制的test按钮,就不会在控制台输出
10 $(e).before(newbtn);
11 }
12
14 window.onload=function(){
15 $("#test").bind("click",function test_btn(){
16 console.log("小菊花又盛开了");//按F12在控制台查看效果。
17 });
18 }
19 </script>
20 <title>Document</title>
21 </head>
22 <body>
23 <input type="button" id="test" value="test" >
24 <input type="button" value="clone" onclick="clone_btn(this)">
25 <div></div>
26 </body>
27 </html>
最终效果就是

漫思

浙公网安备 33010602011771号