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
How to select child elements under id in jQuery
提问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 h1
element 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 h1
element found within the #container
element, or:
它选择h1
在元素中找到的每个#container
元素,或者:
$('#container > h1')
Which selects only those h1
elements that have the #container
element 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 h1
element:
您还可以将其限制为特定h1
元素:
$('#container h1:first')
Which selects only the firsth1
element within the #container
element.
它只选择元素中的第一个元素。h1
#container
References:
参考: