Siblings () method
. siblings ([selector])
This method gets all the sibling elements before and after each matching element, and then returns as a jquery wrapper set. You can also pass a selector expression parameter to it and filter among those sibling elements.
Next, look at two examples:
| The code is as follows |
Copy Code |
<ul> <li> First li</li> <li class= "Li2" > Second li</li> <li> a third li</li> </ul> <ul> <li> First li</li> <li class= "Li2" > Second li</li> <li> a third li</li> </ul> $ (". Li2"). Siblings (). CSS ("Background", "#FF0000"); |
The results of the above jquery code are:
The background of the first and third Li in the two UL will turn red.
Cases
| The code is as follows |
Copy Code |
<ul> <li> First li</li> <li class= "Li2" > Second li</li> <li> a third li</li> </ul> <ul> <li> First li</li> <li class= "Li2" > Second li</li> <li> a third li</li> </ul> <script> $ (". Li2"). Siblings (). CSS ("Background", "#FF0000"); </script> |
The example of the plane is not passed in the selector expression parameter, then give the. Siblings method to pass the parameter and see what the result is.
jquery Code 2:
| The code is as follows |
Copy Code |
$ (". Li2"). Siblings (": a"). CSS ("Background", "#FF0000"); |
The result of the above jquery code running is:
The first one in the UL background color turns red
Cases
| code is as follows |
copy code |
| <ul> <li> First li</li> <li class= "Li2" > Second li</li> <li> Third li</li> </ul> <ul> <li> First Li</li> <li class= "Li2" > Second li</li> <li> Third Li</li> </ul> <script> $ (". Li2"). Siblings (": First"). CSS ("Background", "#FF0000"); </script> |