Jquery: In-depth understanding of first-child selector

黄舟
Release: 2017-06-23 14:28:30
Original
1455 people have browsed it

Overview

Match the first child element

':first' matches only one element, while this selector will match one child for each parent element Element

Example

Description:

Find the first li

HTML code in each ul:

  • John
  • Karl
  • Brandon
  • Glen
  • Tane
  • Ralph
Copy after login

jQueryCode:

$("ul li:first-child")
Copy after login

Result:

[ 
  • John
  • ,
  • Glen
  • ]
    Copy after login

    $("p:first-child")Definition:

    Select the first p element belonging to its parent element and all sibling elements:

    Example code

       first-child选择器   
    

    the first paragraph in body

    The first paragraph in div.

    the last paragraph in div.


    This is a span element.

    The first paragraph in another div.

    The last paragraph in another div.

    Copy after login

    DOM tree structure

    Jquery: In-depth understanding of first-child selector

    ##Popular explanation

    First locate the parent element of all p elements in the current document, then locate its first child element, and then modify the back shadow effect. $("p:last-child") is just the opposite of this , if the first element is not p, but a span element, the back shadow effect will not be modified.

    Extension

    $("p:nth-child(n)")

    Select each p element that belongs to the nth child element of its parent element.
    If the n-th element is not a p element, it does not belong to the range selected by this selector.

    $(“p:nth-last-child(n)”)

    Select the p element that is the third child element of its parent element, counting from the last child element
    If counting down If the nth element is not a p element, it does not belong to the range selected by this selector.


    The above is the detailed content of Jquery: In-depth understanding of first-child selector. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    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
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!