clone()方法:
clone()方法中传入的参数值为布尔型,决定了是否为深层复制,若为深层复制(传入true),则同时复制原标签中添加的事件;若不为深层复制(传入false)则不复制标签中添加的事件。
代码示例一:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
</head>
<body>
<input type="button" value="按钮"/>
<script>
$("[type='button']").bind("click",function(){
console.log("按钮");
})
$("[type='button']").after($("[type='button']").clone(true));//clone方法传入false或不传入值,则后插入的按钮没有点击事件
</script>
</body>
</html>
运行结果:
可见,复制按钮中的时间也被复制。
代码示例二:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
</head>
<body>
<input type="button" value="按钮"/>
<script>
$("[type='button']").bind("click",function(){
console.log("按钮");
})
$("[type='button']").after($("[type='button']").clone(false));//clone方法传入false或不传入值,则后插入的按钮没有点击事件
</script>
</body>
</html>
运行结果:
可见,复制按钮不起作用。