jQuery 如何使用jquery在div内的div内选择div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7305175/
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 div inside div inside div with jquery
提问by Gogol
<div id="tab">
<div class="" style="margin: 10px;">
<div id="someVerylongId1" style="height: 400px; position: relative;">
</div>
</div>
<div class="" style="margin: 10px;">
<div id="someVerylongId2" style="height: 400px; position: relative;">
</div>
</div>
<div class="" style="margin: 10px;">
<div id="someVerylongId3" style="height: 400px; position: relative;">
</div>
</div>
<div>
I want to select all divs not specifying ids or checking any another attributes, is it possible to do like that?
我想选择所有不指定 id 或检查任何其他属性的 div,是否可以这样做?
Here is my try:
这是我的尝试:
$("#tab div div")
but looks like is selecting not exactly correct. Need help.
但看起来选择不完全正确。需要帮忙。
The problem is, my selector returns more elements that it should
问题是,我的选择器返回了它应该返回的更多元素
采纳答案by Henry
$("div > div", "#tab");
That will select all children of divs using the context of #tab
这将使用 #tab 的上下文选择 div 的所有子级
回答by Jordi
Try this
尝试这个
$("#tab > div > div")
You can use child selector (>) for select the child. See more info: http://api.jquery.com/child-selector/
您可以使用子选择器 ( >) 来选择子项。查看更多信息:http: //api.jquery.com/child-selector/
回答by Amir
You could also use find method of JQuery. Find will return all the descendant elements of the selected element.
您也可以使用 JQuery 的 find 方法。Find 将返回所选元素的所有后代元素。
$(selector).find(filter criteria)
$(selector).find(过滤条件)
ex:
前任:
$("div#tab").find("div")
$("div#tab").find("div")