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

How to delete dom matching elements in jquery

青灯夜游
青灯夜游Original
2022-05-18 20:00:295111browse

Jquery method to delete dom matching elements: 1. Use "$()" to select dom elements, the syntax "$("selector")" will return a jquery object containing matching elements; 2. Use remove () deletes the matching element. The syntax "selected element object.remove()" will delete the element and its internal content.

How to delete dom matching elements in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

jquery deletes dom matching elements

1. Use $() to select dom elements

$() can be $(expression), which is a css selector, Xpath or html element, that is, the target element is matched through the above expression.

Syntax:

$("选择器")

For example: $("a")The object constructed uses a CSS selector to construct a jQuery object - it selects all The tag.

2. Use remove() to delete matching elements

The remove() method can delete the selected element and all its contents.

被选元素对象.remove()

Example: Select the div element and delete

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

		<div>
			<p>这是 div 元素中的段落。</p>
			<p>这是 div 元素中的段落。</p>
			<p>这是 div 元素中的段落。</p>
		</div>
		<button>删除div元素</button>

	</body>
</html>

How to delete dom matching elements in jquery

[Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of How to delete dom matching 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