jquery Basics Tutorial Traversal node siblings () method usage instructions

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.