質問

Given a DOM node, I want to drill down the DOM tree to find any nodes by a given class name, say myClass

For example

<div class="container">
  <div class="red">Red One</div>
  Some Text
    <div>
        <div class="myClass"></div>
    </div>
  <div class="blue">Blue One</div>
  <div class="red">Red Two</div>
  <div class="blue">Blue Two</div>
</div>

I have tried using dojo.query(".container").children(".myClass"); which doesnt seem to get a handle on it.

I noticed dojo.query(".container").children(); will only fetch the immediate children, is there an easy way to get all descendants, grandchildren if you will, and not just immediate children?

EDIT

so I realise the question isn't exactly as I had planned, but I will keep the original in place to allow for other users that might benefit from it.

My issue is that rather than getting a handle on the first node by the class name, I want to assume I have got a handle on a particular set of nodes with that class name, but maybe not all of them.

So for example what I would have liked to use, to keep in line with what is written above, would be,

array.forEach(containerNodes, function(node){
    dojo.query(node).children(".myClass")
});

if only the children() function traversed more than one tree level.

So for this scenario, I used dojo.query(".myClass", node); which worked.

But as mentioned below, the likes of dojo.query(".container .myClass"); will work for using class names

役に立ちましたか?

解決

children() is just like in jQuery, where it only gives you immediate children. If you want to create a "subquery" (like jQuery's find()), you will have to use query(), for example:

dojo.query(".container").query(".myClass");

I also made a small fiddle demonstrating it (however, it's in Dojo 1.9 syntax).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top