jQuery Learning-DOM Chapter (5): jQuery uses the clone() method to copy the DOM

Use the clone() method to clone the specified DOM element. Unlike the native cloneNode() method, if the method passed in the clone method is true, the DOM structure, attributes, events, and data will be cloned. And the native cloneNode method, even if true is passed, it can only copy the DOM and content, and cannot copy events and attributes.

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="jquery-1.12.4.js"></script>
	<script type="text/javascript">
		$('document').ready(function (){
     
     
			$.data($('.div1')[0],'name','xuzk');

			$('.div1').click(function (){
     
     
				alert($(this).html());
				console.log($.data($('.div1')[0]));
			})

			$('#btn1').click(function(){
     
     
				$('body').append($('body .div1').clone(true));
			})
		})
	</script>
</head>
<body>
	<input type="button" value="克隆" id="btn1" style="margin-top: 20px;">
	<div class="div1" flagx='x' style="width: 100px;height: 20px;margin-top: 20px;">测试</div>
</body>
</html>

Guess you like

Origin blog.csdn.net/xzk9381/article/details/113609799