Home  >  Article  >  Web Front-end  >  How to remove an element from the end in jquery

How to remove an element from the end in jquery

青灯夜游
青灯夜游Original
2021-11-15 17:23:513258browse

Removal method: 1. Use the "eq(-1)" selector and remove() method, syntax "$(Element).eq(-1).remove()"; 2. Use " :last" selector and remove() method, syntax "$(Element:last).remove()".

How to remove an element from the end in jquery

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

jquery removes an element from the end

Method 1: "eq(-1)" selector remove() method

  • Use the "eq(-1)" selector to select the last element from the end

  • Use the remove() method to delete the selection Element

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-3.6.1.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("p").eq(-1).remove();
			});
		</script>

	</head>
	<body>

				<p>第一段</p>
				<p>第二段</p>
				<p>第三段</p>
				<p>第四段</p>
				<p>第五段</p>
				<p>第六段</p>

	</body>
</html>

How to remove an element from the end in jquery

Method 2: ":last" selector remove() method

  • Use the ":last" selector to select the last element from the end

  • Use the remove() method to delete the selected element

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-3.6.1.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("p:last").remove();
			});
		</script>

	</head>
	<body>
		<html>
			<body>
				<p>第一段</p>
				<p>第二段</p>
				<p>第三段</p>
				<p>第四段</p>
				<p>第五段</p>
			</body>
		</html>


	</body>
</html>

How to remove an element from the end in jquery

Recommended related tutorials: jQuery video tutorial

The above is the detailed content of How to remove an element from the end 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