Home >Web Front-end >JS Tutorial >Detailed explanation of the difference between :first-child, :first and :first-of-type in jQuery selectors

Detailed explanation of the difference between :first-child, :first and :first-of-type in jQuery selectors

黄舟
黄舟Original
2017-06-23 14:39:011421browse

Let’s start with a little digression. Because my first job after graduation was not a front-end job, it was in a state-owned enterprise. After working for about 3 months, I really couldn’t stand the comfortable pace of life, so I resigned decisively (actually because of the lack of money), and then I chose Decide on the direction you like and start from the page. So now it is almost the third month of working as a page boy. Because I found an internship at a small start-up company, I actually have relatively little knowledge about the front-end from beginning to end. I usually just check and learn whatever I need. , so I can only look for jQuery API recently.

Okay, let’s get to the point.

I saw the :first-child selector in the API, which is described in this way (I used someone else’s translation and compilation, but I still feel like reading the official English version It's better, but I still use this version because I'm lazy)

Regarding this, I was very annoyed at first, because what is returned is an array# of an object ## (I personally understand this, I don’t know if it is correct), but the name of this selector is: first-child (I really feel like complaining, why don’t I call it children). Later, I tried it myself and found that if I don’t use each( ) is traversed, the first element of the array is returned by default, which is 25edfb22a4f469ecb59f1190150159c6Johnbed06894275b65c1ab86501b08a632eb in the screenshot. (Actually, this is not what I want to talk about, this is just to pave the way)

In this API, if you look back a little further, you will see the selector

:first-of-type, as follows It’s his description of this selector

Later, because of what he said at the beginning of this part, I went to Baidu to do some online research: first-of-type description, and then also checked: first-child. I found that some of the instructions seemed to be inaccurate.

Okay, there are too many words, so let’s go straight to the code and effects. (The code part, the introduction of jQuery, I don’t want to bother to find the one on the Internet, just use the local path)

【:first】

<!DOCTYPE html><html lang="zh-CN"><head>
    <title>test</title>
    <meta charset="utf-8">
    <script src="./js/jquery-1.11/jquery.min.js"></script></head><body>
    <p>
        <ul>
            <li><p>测试li-11</p></li>
            <li><p>测试li-12</p></li>
            <li><p>测试li-13</p></li>
        </ul>
        <hr>
        <ul>
            <li><p>测试li-21</p></li>
            <li><p>测试li-22</p></li>
            <li><p>测试li-23</p></li>
        </ul>
        <hr>
    </p>
    <hr>
    <ul>
        <li><p>测试li-31</p></li>
        <li><p>测试li-32</p></li>
        <li><p>测试li-33</p></li>
    </ul>
    <script>
        $(document).ready(function(){
            $("ul li:first").each(function(){
                alert($(this).html());
            })
        })    </script></body></html>

【:first selector in several browsers Display under the browser】

【:first-child】

<!DOCTYPE html><html lang="zh-CN"><head>
    <title>test</title>
    <meta charset="utf-8">
    <script src="./js/jquery-1.11/jquery.min.js"></script></head><body>
    <p>
        <ul>
            <li><p>测试li-11</p></li>
            <li><p>测试li-12</p></li>
            <li><p>测试li-13</p></li>
        </ul>
        <hr>
        <ul>
            <li><p>测试li-21</p></li>
            <li><p>测试li-22</p></li>
            <li><p>测试li-23</p></li>
        </ul>
        <hr>
    </p>
    <hr>
    <ul>
        <li><p>测试li-31</p></li>
        <li><p>测试li-32</p></li>
        <li><p>测试li-33</p></li>
    </ul>
    <script>
        $(document).ready(function(){
            $("ul li:firstchild").each(function(){
                alert($(this).html());
            })
        })    </script></body></html>

【:first-child selector in each browser The effect below] (Because of the traversal, there are 3 selected elements, that is, 3 pop-up windows will pop up; the browsers are: waterfox, chrome, IE 11, Edge)

[ waterfox】

【chrome】PS: There seems to be a pop-up window under chrome, so I can’t switch to another window, so I can only take 3 screenshots and stitch them together

【IE 11】

## 【Edge】

【:first-of-type】

<!DOCTYPE html><html lang="zh-CN"><head>
    <title>test</title>
    <meta charset="utf-8">
    <script src="./bootstrap/js/jquery-1.11/jquery.min.js"></script></head><body>
    <p>
        <ul>
            <li><p>测试li-11</p></li>
            <li><p>测试li-12</p></li>
            <li><p>测试li-13</p></li>
        </ul>
        <hr>
        <ul>
            <li><p>测试li-21</p></li>
            <li><p>测试li-22</p></li>
            <li><p>测试li-23</p></li>
        </ul>
        <hr>
    </p>
    <hr>
    <ul>
        <li><p>测试li-31</p></li>
        <li><p>测试li-32</p></li>
        <li><p>测试li-33</p></li>
    </ul>
    <script>
        $(document).ready(function(){
            $("ul li:first-of-type").each(function(){
                alert($(this).html());
            })
        })    </script></body></html>

【:first-of-type selector display effect in each browser】

The display of this part is the same as that in [:first-child 】The effect is the same under the selector, but the length is reduced, and screenshots are still not included.

[Summary]

From the screenshot above, we can see that :first , :first-child , :first-of-type, these three selectors, browse The different processor cores will not affect jQuery and display effects.

On the other hand,

The [:first] selector returns a single node, that is, the node, which is unique and certain

【:first-child】The selector actually returns an array of nodes. If each is not used for traversal, the first node of the array is returned by default (later I checked the return value of size() , it is indeed 3, which confirms my idea)

[:first-of-type] selector, similar to [:first-child] (the reason why it is not the same is because the result is the same in this example , and the two selectors have different functions), and what is returned is also an array of nodes. If each is not used for traversal, the first node of the array is returned by default (later I checked the return value of size() , it is indeed 3, which confirms my idea).

PS: There seems to be a last corresponding to first. I haven’t seen it yet, but I personally feel that the two are the same, except that one is the first and the other is the last. It's the last one. If I have time, I will send the verification results of the last three selectors

The above is the detailed content of Detailed explanation of the difference between :first-child, :first and :first-of-type in jQuery selectors. 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