loop over all descendants yui3 / javascript
I am trying to determine if a click occurs inside of a certain node. My idea for doing this is to take the node and loop over all of its descendants开发者_StackOverflow社区, testing if each node is the click events target.
I'm not sure what the best way to get all the descendants of a specified node is in YUI3. I am open to doing it in just plain JS if it's to complicated in yui3. I'm also open to another approach if anyone has a better way of doing this.
Thanks in advance for the help.
var isChild = wrapperNode.contains(targetNode);
http://developer.yahoo.com/yui/3/api/Node.html#method_contains
What you want to create, sounds an awful lot like click delegation.
I don't do YUI, but you can read up on event delegation in YUI3 here:
http://www.yuiblog.com/blog/2009/11/13/event-delegation-3/
http://kickballcreative.com/yui/demos/event-delegation/
My current solution (in YUI3) just in case anyone has the same problem. Feel free to post improvements upon it.
function isChild(targetNode, wrapperNode) {
var descendants = wrapperNode.all('*');
var counter = 0;
descendants.each(function(node) {
if(node == targetNode) {
counter++;
}
});
return (counter > 0) ? true : false;
}
If I understand the situation, the node that you are interested in contains some nodes that might receive clicks. If such is the case, the click event will bubble up to the parent node, so listening for a click on that node should be enough.
Then again it might be something more complicated that you need to do, in which case, apologies if I misunderstood.
精彩评论