Home  >  Article  >  Web Front-end  >  How to delete parent element based on specified element in jquery

How to delete parent element based on specified element in jquery

青灯夜游
青灯夜游Original
2021-11-15 17:54:092707browse

In jquery, you can use the unwrap() method to delete the parent element of the specified element. This method can remove the parent element of the selected element, but will keep itself in its original position; the syntax "$( selector).unwrap()".

How to delete parent element based on specified element in jquery

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

In jquery, you can use the unwrap() method to delete the parent element based on the specified element.

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

Syntax:

$(selector).unwrap()

Note: This method does not accept any parameters.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("p").unwrap();
				});
			});
		</script>
		<style>
			div {
				background-color: yellow;
			}

			article {
				background-color: pink;
			}
		</style>
	</head>
	<body>

		<div>
			<p>这是一个div元素中的段落。</p>
		</div>
		<article>
			<p>这是一个在文章元素的段落。</p>
		</article>
		<button>移除每个p元素的父元素</button>

	</body>
</html>

How to delete parent element based on specified element in jquery

Recommended related tutorials: jQuery video tutorial

The above is the detailed content of How to delete parent element based on specified element 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