Home  >  Article  >  Web Front-end  >  How to delete superior elements in jquery

How to delete superior elements in jquery

青灯夜游
青灯夜游Original
2022-03-10 19:04:282621browse

Deletion method: 1. Use the unwrap() method to delete, the syntax is "$(selector).unwrap()"; 2. Use parent() to get the superior element, and use the remove() method to delete the obtained element. , syntax "$(selector).parent().remove()".

How to delete superior elements in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jquery deletes the superior element (parent element)

Method 1: Use the unwrap() method

# The ##unwrap() method removes the parent element of the selected element, but leaves itself (and siblings, if present) in its original position.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("li.start").unwrap();
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (父节点)
				<li>li (类名为"star"的上一个兄弟节点)</li>
				<li>li (类名为"star"的上一个兄弟节点)</li>
				<li class="start">starli (兄弟节点)</li>
				<li>li (类名为"star"的下一个兄弟节点)</li>
				<li>li (类名为"star"的下一个兄弟节点)</li>
			</ul>
		</div>
		<button>删除“star”的li元素的上级元素</button>
	</body>
</html>

How to delete superior elements in jquery

Method 2: Use parent() to obtain the superior element, and use the remove() method to delete the obtained element

remove () method removes selected elements, including all text and child nodes. This method also removes data and events from the selected element.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("li.start").parent().css({
					"color": "red",
					"border": "2px solid red"
				});
				$("button").click(function() {
					$("li.start").parent().remove();
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (父节点)
				<li>li (类名为"star"的上一个兄弟节点)</li>
				<li>li (类名为"star"的上一个兄弟节点)</li>
				<li class="start">starli (兄弟节点)</li>
				<li>li (类名为"star"的下一个兄弟节点)</li>
				<li>li (类名为"star"的下一个兄弟节点)</li>
			</ul>
		</div>
		<p>选择类名称为“star”的li元素的上级元素</p>
		<button>删除上级元素(包含子元素)</button>
	</body>
</html>

How to delete superior elements in jquery

[Recommended learning:

jQuery video tutorial, web front-end video

The above is the detailed content of How to delete superior elements in jquery. 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