Parent and parents of jquery

Source: Internet
Author: User

 

About parent and parents of jquery

A parent is a set of elements that obtain a unique parent element that contains all matching elements.
Parents is a set of elements (excluding the root element) that contain the ancestor elements of all matching elements ). You can use an optional expression to filter data.
It can be seen that parent is clear, that is, the parent element of the current element; parents is the ancestor element of the current element. The following are examples:
<Div
Id = 'did1'>
<Div id = 'div2'> <p> </P> </div>
<Div
Id = 'did3' class = 'A'> <p> </P> </div>
<Div
Id = 'div4 '> <p> </P> </div>
</Div>

$ ('P'). Parent () obtains div2, div3, div4
$ ('P'). Parent ('. A') obtains div3.
$ ('P'). Parent (). Parent () gets div1, which is odd. However, the characteristics of jquery objects determine that this is feasible.
$ ('P'). Parents () obtains div1, div2, div3, and div4.
$ ('P'). Parents ('. A') obtains div3.

Jquery's selector is really powerful. I think jquery's most comfortable to use is its selector and filtering function.





 

  1. <
    Html
    >

  2. <
    Head
    >
    </
    Head
    >

  3. <
    Body
    >

  4. <
    Div
    ID
    = "D1"
    >

  5. <
    P
    >
    Hello
  6. <
    Div
    ID
    = "CRD"
    >
    XXXXX </
    Div
    >

  7. </
    P
    >

  8. </
    Div
    >

  9. <
    Div
    Class
    = "Selected"
    >

  10. <
    P
    >
    Hello again </
    P
    >

  11. </
    Div
    >
     

  12. </
    Body
    >

  13. </
    Html
    >



  14. Parent ([expr])
  15. Note:
  16. Expr (string): (optional) filtering expression
  17. Obtains a set of elements that contain the unique parent element of all matching elements.
  18. Example:
  19. <
    Pre
    Class
    = JS
    Name
    = "Code"
    >
    $ ("P"). Parent ()
  20. </
    Pre
    >

  21. <
    BR
    >
    Result:
  22. <
    BR
    >
    <
    Pre
    Class
    = Html
    Name
    = "Code"
    >
    <
    Div
    ID
    = "D1"
    >
    </
    Div
    >
    <
    Div
    Class
    = "Selected"
    >
    </
    Div
    >
    </
    Pre
    >

  23. <
    BR
    >

  24. <
    BR
    >
    Parents
    ([Expr])

  25. <
    BR
    >
    Note:
  26. <
    BR
    >
    Obtains a set of elements (excluding the root element) that contain all the ancestor elements matching the elements ). You can use an optional expression to filter data.

  27. <
    BR
    >

  28. <
    BR
    >
    It mainly refers to the understanding of ancestor elements.
  29. <
    BR
    >

  30. <
    BR
    >
    <
    Pre
    Class
    = JS
    Name
    = "Code"
    >
    </
    Pre
    >

  31. <
    BR
    >
    $ ("P"). Parents
    ()

  32. <
    BR
    >

<HTML> 
  
  • $ ("P"). Parent ()
  • $("p").parent()

    Result:
    HTML code

    1. <
      Div
      ID
      = "D1"
      >
      </
      Div
      >
      <
      Div
      Class
      = "Selected"
      >
      </
      Div
      >
    <div id="d1"></div><div class="selected"></div>

    Parents
    ([Expr])

    Note:

    Obtains a set of elements (excluding the root element) that contain all the ancestor elements matching the elements ). You can use an optional expression to filter data.

    It mainly refers to the understanding of ancestor elements.

    JS Code

    $ ("P"). Parents
    ()


    Result: div, body, HTML, and <Div
    Class = "selected"> </div> element.

    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.