closest should return the deepest match for each selector
For performance reasons, and for what I considered expected results, closest should return the first match per selector. This is causing problems with event delegation with nested DOM structures. For example:
Consider a nested menu like:
- <ul><li>
- Option 1
- <ul><li>Option 2</li></ul>
- </li>
And the following code
- $('li').live('click', function(){
- $(this).addClass('clicked')
- })
If you click on the deep li, the event handler function will be called on both
LI's. This makes using event delegation for nested structures (like a menu) pretty much unusable. I've used event delegation for a while, and I've never wanted to respond to anything other than the deepest child.
Further, by stopping walking up the parentNodes when all selectors have been filled, it's very likely the 'match' case can be speed up.
So ...
I recommend closest only returning the first matched parent element, making delegation only callback on the deepest child.