Home > Web Front-end > JS Tutorial > How to delete the previous element in jquery

How to delete the previous element in jquery

青灯夜游
Release: 2021-11-16 16:45:51
Original
3107 people have browsed it

Jquery method to delete the previous element: 1. Use the prev() method to obtain the previous element object of the specified element, syntax "$(selector).prev()"; 2. Use the remove() method to delete The previous element object obtained, the syntax is "previous element object.remove()".

How to delete the previous element in jquery

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

jquery deletes the previous element

In jquery, if you want to delete the previous element, you first need to get the previous element, this is Need to use prev() method.

prev() method returns the previous sibling element of the selected element. (Sibling elements are elements that share the same parent element.)

To obtain the previous element, you need to use the remove() method to delete it.

Implementation code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("#but1").click(function() {
					$("#box").prev().remove();
				});
			});
		</script>

	</head>
	<body>
		<p>第一段</p>
		<p>第二段</p>
		<p>第三段</p>
		<p id="box">第四段--#box元素</p>
		<p>第五段</p>
		<p>第六段</p>
		<button id="but1">删除 #box元素 前一个元素</button> 
	</body>
</html>
Copy after login

How to delete the previous element in jquery

Related tutorial recommendations: jQuery video tutorial

The above is the detailed content of How to delete the previous element in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template