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

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

How to select div inside div inside div with jquery

jqueryjquery-selectors

提问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 的所有子级

http://jsfiddle.net/HenryGarle/mHpMM/

http://jsfiddle.net/HenryGarle/mHpMM/

回答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")

回答by kritya

$("#tab").siblings();

[docs]

[文档]

Quote from jquery:

来自 jquery 的引用:

Get the siblings of each element in the set of matched elements, optionally filtered by a selector.

获取匹配元素集中每个元素的同级元素,可选择由选择器过滤。