jQuery中的clone()方法

该方法的作用就是复制一个一模一样的DOM节点,但是有一点需要注意就是该方法可以传入一个布尔型参数,如果传入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>

发布了99 篇原创文章 · 获赞 93 · 访问量 5217

猜你喜欢

转载自blog.csdn.net/DangerousMc/article/details/103001084
今日推荐