This article mainly introduces jQuery's method for searching peer elements. examples show how to use methods such as next, nextAll, nextUtil, prev, and prevAll, A complete example is provided, which has some reference value. If you need it, you can refer to the example below to describe jQuery's method of searching peer elements. Share it with you for your reference. The specific analysis is as follows:
1. next () method
Used to search for a single peer element that follows each matching element. You can also specify a selector to filter peer elements as needed. The syntax format is as follows:
The Code is as follows:
Next ([selector])
$ ("P"). next ("p" ).css ("color", "# FCF ");
2. nextAll () method
Used to search for all peer elements that follow each matching element. You can also specify a selector to filter peer elements as needed.
The Code is as follows:
NextAll ([selector])
$ ("P" ).nextAll().css ("color", "blue ");
3. nextUtil () method
Used to obtain the peer element that follows each matching element until it matches the element of the given selector (but does not include this element). The format is as follows:
The Code is as follows:
NextUtil ([selector])
Instance:
The Code is as follows:
$ ("# P1"). nextUtil ("p" ).css ("border", "1px solid red ");
Set the border of all the intermediate peer elements whose id is p1 to red when the next p element ends.
4. prev () method
Used to search for a single peer element next to each matching element. You can also specify a selector to filter peer elements as needed. The syntax format is as follows:
The Code is as follows:
Prev ([selector])
Instance:
The Code is as follows:
$ ("# P2"). prev ("span" ).css ("color", "blue ");
The font of the span element next to the p2 element changes to blue.
5. prevAll () method
This interface is used to search all peer elements before the current element. You can also specify a selector to filter peer elements. The syntax format is as follows:
The Code is as follows:
PrevAll ([selector])
6. prevUtil () method
It is used to search all peer elements before the current element until the matching element is met. The syntax format is as follows:
The Code is as follows:
PrevUtil ([selector])
$ ("# P2"). prevUtil ("input" ).css ("color", "red ");
7. siblings () method
It is used to search for all peer elements of each matching element. You can also specify a selector to filter these peer elements as needed. The format is as follows:
The Code is as follows:
Siblings ([selector])
$ ("P" ).siblings().css ("color", "red ");
Example:
The Code is as follows: