Home > Web Front-end > JS Tutorial > jquery hierarchical selector

jquery hierarchical selector

无忌哥哥
Release: 2018-06-29 11:11:21
Original
1850 people have browsed it

//1. Descendants: Space

  $('li a').addClass('green')
Copy after login

//2. All child elements>

//Only the foreground color of ul's child element li turns red, and the grandson element< a>The text will not change

$(&#39;ul > *&#39;).addClass(&#39;red&#39;)
Copy after login

//If separated by spaces, the foreground color of

  • will all change

    $(&#39;ul  *&#39;).css(&#39;color&#39;,&#39;red&#39;)
    Copy after login

    //3. Adjacent sibling elements

    Change the next brother of the 5th li: the foreground color of the 6th li to green

    $(&#39;li:nth-child(5) + li&#39;).addClass(&#39;green&#39;)
    Copy after login

    //4. All sibling elements~

    $(&#39;li:nth-child(5) ~ li&#39;).addClass(&#39;green&#39;)
    Copy after login

    //5 .The first and last element

      $(&#39;li:first-child&#39;).addClass(&#39;green&#39;)
      $(&#39;li:first&#39;).addClass(&#39;green&#39;)
      $(&#39;li:last-child&#39;).css(&#39;color&#39;,&#39;red&#39;)
      $(&#39;li:last&#39;).css(&#39;color&#39;,&#39;red&#39;)
    Copy after login

    //6. Directly select an element

      $(&#39;li:nth-child(6)&#39;).addClass(&#39;red&#39;)
      //jquery使用eq(i),i从0开始,注意与css中的不一样
      $(&#39;li:eq(5)&#39;).addClass(&#39;red&#39;)
    Copy after login

    //7Select an element greater than or less than a certain serial number

    / /First remove the class on all elements

      $(&#39;*&#39;).removeClass()
    Copy after login

    //Only remove li, excluding the a under li, the link is still green

      $(&#39;li&#39;).removeClass()
    Copy after login

    //Select all elements with serial numbers greater than 4, please note Calculate from 0

    $(&#39;li:gt(3)&#39;).addClass(&#39;red&#39;)
    Copy after login

    //Select all elements with serial numbers less than 8

    $(&#39;li:lt(7)&#39;).addClass(&#39;red&#39;)
    Copy after login

    //Select elements based on serial number characteristics

    //Select all elements with even serial numbers even

    //Because it starts from 0, 0, 2,, 4, so it looks like you have chosen an odd number, please pay attention

    $(&#39;li:even&#39;).addClass(&#39;red&#39;)
    Copy after login

    //You may have guessed, choose an odd number to use It's odd, of course, you are right

      $(&#39;li:odd&#39;).addClass(&#39;red&#39;)
    Copy after login

    The above is the detailed content of jquery hierarchical selector. For more information, please follow other related articles on the PHP Chinese website!

  • 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