Home > Web Front-end > Front-end Q&A > What are the ways to find sibling elements in jquery?

What are the ways to find sibling elements in jquery?

青灯夜游
Release: 2022-05-25 17:50:38
Original
4701 people have browsed it

There are seven methods: 1. siblings(), which can obtain all elements of the same generation; 2. next(), which can obtain the next sibling elements; 3. nextAll(), which can obtain all the elements of the next sibling. ; 4. prev(), you can get the upper level sibling elements; 5. prevAll(), you can get all the upper level sibling elements, etc.

What are the ways to find sibling elements in jquery?

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

There are seven methods for jquery to find sibling elements: siblings(), next(), nextAll(), nextUntil(), prev(), prevAll(), prevUntil()

1. Siblings() method

is mainly used to obtain all elements of the specified element’s peers

<!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").siblings().css({
					"color": "red",
					"border": "2px solid red"
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul 
				<li>li (同辈节点)</li>
				<li>li (上一个同辈节点)</li>
				<li class="start">li (本元素)</li>
				<li>li (下一个同辈节点)</li>
				<li>li (同辈节点)</li>
			</ul>
		</div>

	</body>
</html>
Copy after login

What are the ways to find sibling elements in jquery?

2. next( ) method

is mainly used to obtain the next sibling element of the specified element

$(document).ready(function() {
	$("li.start").next().css({
		"color": "red",
		"border": "2px solid red"
	});
});
Copy after login

What are the ways to find sibling elements in jquery?

3. nextAll() method

Mainly used to obtain all elements of the next sibling of the specified element

$(document).ready(function() {
	$("li.start").nextAll().css({
		"color": "red",
		"border": "2px solid red"
	});
});
Copy after login

What are the ways to find sibling elements in jquery?

##4. nextUntil() method

Mainly used to obtain the next sibling element of the specified element. This sibling element must be the element between the specified element and the element set by the nextUntil() method.

5. prev() method

Mainly used to obtain the upper-level sibling elements of the specified element

$(document).ready(function() {
	$("li.start").prev().css({
		"color": "red",
		"border": "2px solid red"
	});
});
Copy after login

What are the ways to find sibling elements in jquery?

##6, prevAll() method

Mainly used to obtain all sibling elements at the upper level of the specified element

$(document).ready(function() {
	$("li.start").prevAll().css({
		"color": "red",
		"border": "2px solid red"
	});
});
Copy after login

What are the ways to find sibling elements in jquery?##7. prevUntil() method

Mainly used to obtain the previous sibling element of the specified element. This sibling element must be the element between the specified element and the element set by the prevUntil() method

[Recommended learning:

jQuery video tutorial

web front-end video

The above is the detailed content of What are the ways to find sibling elements 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