jQuery 如何在jQuery中选择id下的子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11819622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:58:46  来源:igfitidea点击:

How to select child elements under id in jQuery

jquerycss-selectors

提问by kvu787

Sample code:

示例代码:

<div id='container'>
  <h1>heading 1</h1>
  <h2>heading 2</h2>
  <p>paragraph</p>
</div>

I would like a jQuery equivalent of this CSS selector to select the h1element under the div:

我想要一个与此 CSS 选择器等效的 jQuery 来选择以下h1元素div

#container h1 {}

回答by David says reinstate Monica

You can simply implement the exact same selector:

您可以简单地实现完全相同的选择器

$('#container h1')

Which selects every h1element found within the #containerelement, or:

它选择h1在元素中找到的每个#container元素,或者:

$('#container > h1')

Which selects only those h1elements that have the #containerelement as their parent (notgrandparent, or other form of ancestor).

它只选择那些h1#container元素作为其父元素(不是祖父元素或其他形式的祖先元素)的元素。

Or, if you prefer:

或者,如果您更喜欢:

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

You could also restrict it to a particular h1element:

您还可以将其限制为特定h1元素:

$('#container h1:first')

Which selects only the firsth1element within the #containerelement.

它只选择元素中的第一个元素。h1#container

References:

参考: