and remove empty method in jQuery

In jQuery, empty () and remove () methods are used to remove DOM node:

empty (): a collection of elements that were removed matched all child nodes, excluding itself.

remove ([expr]): Delete matched elements, including their contents and node. 

The difference is that the elements themselves whether to delete the following two examples show the effect of these two methods:

The following code tag selector acquires first p tag, then call empty () method with p, label, delete p is wrapped labels of all sub-tab results are as follows, where only one p tag:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<p><span>I </span><b>Love You.</b></p>
		<script>
			$("p").empty();
		</script>
	</body>
</html>

The following code after the call to remove the Tag Chooser acquisition target tag () method is to delete the entire match elements, including inside the parcel, and content itself. P operating results in the entire label elements do not exist, span the div tag also.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<p><span>I </span><b>Love You.</b></p>
		<script>
			$("p").remove();
		</script>
		
		<div><span>I </span><span class="letters">Love You.</span></div>
		<script>
			$("span").remove(".letters");//删除class为letters的所有span标签
		</script>
	</body>
</html>

 

 

Published 99 original articles · won praise 93 · views 5215

Guess you like

Origin blog.csdn.net/DangerousMc/article/details/103015708