Say I have an HTML structure like
<div id="a">
<div id="b">
<div id="c"></div>
</div>
</div>
To do a query for the children of "a" using querySelectorAll I can do something like
//Get "b", but not "c"
document.querySelectorAll('#a > div')
My question is: is it possible to do this without the ID, referencing the node directly? I tried doing
var a_div = document.getElementById('a')
a_div.querySelectorAll('> div') //<-- error here
but I get an error telling me that the selector I used is invalid.
And in case anyone is wondering, my real use case would be something more complicated like '> .foo .bar .baz' so I would prefer to avoid manual DOM traversal. Currently I am adding a temporary id to the root div but that seems like an ugly hack...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…