Home >Web Front-end >JS Tutorial >jQuery interpretation of the difference between empty, remove and detach

jQuery interpretation of the difference between empty, remove and detach

巴扎黑
巴扎黑Original
2017-06-25 09:53:231432browse

    Use a comparison table to explain the differences between several methods

##Selector Yes#Yes (when there is no parameter), if there is a parameter, it should be based on the scope of the parameter. ##detach The following focuses on the feature of detach that does not remove events and data bound to elements.

Method name

Parameters

The event bound to the removed element and Whether the data is also removed

Whether the element itself is removed

empty

##是

No

##remove

Expression

, such as remove(“.

class

”);remove(“#id”);

remove(“tag”):

##The parameters are the same as remove

No

The situation is the same as remove

The code is as follows:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript" src="jquery-1.11.0.js" ></script>
		<script type="text/javascript">
			$(function() {
				var $p2=$("#p2");
				$p2.data("value", 1);
				$("#detach").on("click", function() {
					$p2.detach();
				});

				$("#back").on("click", function() {
					$("#p1").append($p2);
					console.log($("#p2").data("value"));
				});
			});
		</script>
	</head>
	<body>
		<p id="p1">
			<p id="p2">
				p2
			</p>
			<p id="p3">
				p3
			</p>
		</p>
		<input value="detach" id="detach" type="button" />
		<input value="back" id="back" type="button" />
	</body>
</html>

directly The running results are as follows:

## Click detach and the running results are as follows:


##Click back to run the result as shown below:

#If you change detach to remove, then after clicking back, the console will display undefined.

The above is the detailed content of jQuery interpretation of the difference between empty, remove and detach. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn