Home >Web Front-end >JS Tutorial >The difference between nth-child(N) and eq(N) selectors in JQuery

The difference between nth-child(N) and eq(N) selectors in JQuery

黄舟
黄舟Original
2017-06-23 14:04:242768browse

1. nth-child(N): The subscript starts from 1; eq(N): The subscript starts from 0

2. nth-child(N): Select multiple elements; eq( N): Select an element

3, nth-child(N): In a document tree, select all elements ranked Nth in each layer.
Example: nth-child(2): including the second child of the father, the second child of the brother, and the second child of the descendant, the green part in the picture below

The difference between nth-child(N) and eq(N) selectors in JQuery

##eq( N): In a document tree, after sorting in preorder, select the Nth element and all its sub-elements.

Example: In the following HTML code, view the results of $("div:eq(3)").html(). (The number is the number in the previous sequence)

<div>0  
    <div>1  
        <div>div_a_2</div>  
        <div>3  
            <div>div_b_4</div>  
            <div>div_c_5</div>  
        </div>  
        <div>div_d_6</div>  
    </div>  
    <div>7  
        <div>div_e_8</div>  
    </div>  
    <div>9  
        <div>10  
            <div>div_f_11</div>  
        </div>  
        <div>div_g_12</div>  
    </div>  
</div>

$("div:eq(3)").html() selection result

The difference between nth-child(N) and eq(N) selectors in JQuery

Finally Think about a question:

$(&#39;div:nth-child(odd)&#39;).css("color","red");  
$(&#39;div:nth-child(even)&#39;).css("color","blue");

Using the above two sentences, will the font color of all divs appear in red and blue intervals? The answer is of course: no

The test is as follows, the code part:

  
  
  
  
      
  
  
<div>0  
    <div>1  
        <div>div_a_2</div>  
        <div>3  
            <div>div_b_4</div>  
            <div>div_c_5</div>  
        </div>  
        <div>div_d_6</div>  
    </div>  
    <div>7  
        <div>div_e_8</div>  
    </div>  
    <div>9  
        <div>10  
            <div>div_f_11</div>  
        </div>  
        <div>div_g_12</div>  
    </div>  
</div>  
  

Running results:

The difference between nth-child(N) and eq(N) selectors in JQuery

The above is the detailed content of The difference between nth-child(N) and eq(N) selectors in JQuery. For more information, please follow other related articles on the PHP Chinese website!

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