How to select child elements under id in jQuery

后端 未结 1 1916
深忆病人
深忆病人 2020-12-23 21:37

Sample code:

heading 1

heading 2

paragraph

相关标签:
1条回答
  • 2020-12-23 22:06

    You can simply implement the exact same selector:

    $('#container h1')
    

    Which selects every h1 element found within the #container element, or:

    $('#container > h1')
    

    Which selects only those h1 elements that have the #container element as their parent (not grandparent, or other form of ancestor).

    Or, if you prefer:

    $('#container').find('h1')
    

    You could also restrict it to a particular h1 element:

    $('#container h1:first')
    

    Which selects only the first h1 element within the #container element.

    References:

    • find().
    • :first selector.
    • jQuery selectors.
    0 讨论(0)
提交回复
热议问题