How to select div inside div inside div with jquer

2019-03-15 10:45发布

问题:

<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?

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

回答1:

$("div > div", "#tab");

That will select all children of divs using the context of #tab

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



回答2:

Try this

$("#tab > div > div")

You can use child selector (>) for select the child. See more info: http://api.jquery.com/child-selector/



回答3:

You could also use find method of JQuery. Find will return all the descendant elements of the selected element.

$(selector).find(filter criteria)

ex:

$("div#tab").find("div")



回答4:

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

[docs]

Quote from jquery:

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