Home >Web Front-end >JS Tutorial >jQuery search method for sibling elements_jquery

jQuery search method for sibling elements_jquery

WBOY
WBOYOriginal
2016-05-16 16:14:421530browse

The example in this article describes the jQuery method of searching for peer elements. Share it with everyone for your reference. The specific analysis is as follows:

1. next() method

is used to search for a single sibling element immediately following each matching element. You can also specify a selector to filter sibling elements as needed. The syntax format is as follows:

Copy code The code is as follows:
next([selector])
$("p").next("p").css("color", "#FCF");

2. nextAll() method

is used to search for all sibling elements immediately following each matching element. If necessary, you can also specify a selector to filter sibling elements

Copy code The code is as follows:
nextAll([selector])
$("p").nextAll().css("color", "blue");

3. nextUtil() method

is used to get the sibling elements immediately following each matching element, up to (but not including) the element matching the given selector, in the following format:

Copy code The code is as follows:
nextUtil([selector])

Example:

Copy code The code is as follows:
$("#div1").nextUtil("div" ).css("border", "1px solid red");

Set the borders of all intermediate sibling elements from the element with id div1 to the end of the next div element to red

4. prev() method

is used to search for a single sibling element immediately before each matching element. You can also specify a selector to filter sibling elements as needed. The syntax format is as follows:

Copy code The code is as follows:
prev([selector])

Example:

Copy code The code is as follows:
$("#div2").prev("span" ).css("color", "blue");
The font of the span element immediately before the div2 element will become blue.

5. prevAll() method

is used to search for all sibling elements before the current element. You can also specify a selector to filter sibling elements. The syntax format is as follows:

Copy code The code is as follows:
prevAll([selector])

6. prevUtil() method

is used to search all sibling elements before the current element until the matching element is encountered. The syntax format is as follows:

Copy code The code is as follows:
prevUtil([selector])
$("#div2").prevUtil("input").css("color", "red");

7. siblings() method

is used to search all sibling elements of each matching element. If necessary, you can also specify a selector to filter these sibling elements. The format is as follows:

Copy code The code is as follows:
siblings([selector])
$("div").siblings().css("color", "red");

An example is as follows:

Copy code The code is as follows:





Search for sibling elements of the specified element



Hotel room accommodation status table






































Single room (already occupied) Double Room (not staying) Deluxe Suite (Already stayed) Single room (already occupied) Deluxe Suite (not staying)
Single Room (Already occupied) Double Room (not staying) Deluxe Suite (not staying) Single Room (not staying) Deluxe Suite (not staying)
Single Room (not staying) Double Room (not staying) Deluxe Suite (Already stayed) Single Room (Already occupied) Deluxe Suite (not staying)
Single Room (Already occupied) Double Room (Already occupied) Deluxe Suite (not staying) Single Room (not staying) Deluxe Suite (Already stayed)
Single Room (Already occupied) Double Room (not staying) Deluxe Suite (Already stayed) Single Room (Already occupied) Deluxe Suite (not staying)


The rendering is as follows:

I hope this article will be helpful to everyone’s jQuery programming.

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