I think you're missing the point of event delegation. The $.delegate
snippet is just a convenience (and, in my mind, unnecessary).

The following code:

$('#nav a').click($.delegate({
   '.exit: function() { /* do stuff */ },
   '.open: function() { /* do other stuff */ }
}));

Is functionally equivalent (and suspiciously similar) to:

$('#nav a').click(function(){
   if($(this).is('.exit')){ /* do stuff */ };
   if($(this).is('.open')){ /* do other stuff */ };
});

Note that, line by line, this what happens in $.delegate.

...OR might be more efficient run (to avoid the 'is' calls) as:

$('#nav a.exit').click(function(){ /* do stuff */ });
$('#nav a.open').click(function(){ /* do other stuff */ });

$.delegate just provides a slightly different way of doing it (and,
from the above code, I think you can see why I think it's not needed).

Not sure what you mean by 'bubbling up'. If you do this:

$('#nav').click(function(){ /* do stuff */ });

.. then the function applies to the div called nav ONLY. It will do
stuff if you click on a nested element, but only because you're
clicking on the div as well. The nav div is still the target.

>> It might even make sense to capture
>> clicks that bubble up to the document object.

Do you mean something like this:

$('*').click($.delegate({
    '#nav a.exit': function(){ /* do stuff */ },
    '#nav a.open': function(){ /* do other stuff */ }
});

... that looks like a _really_ bad idea.

Thanks

Hamish Campbell

On Feb 21, 9:42 am, hartshorne <[EMAIL PROTECTED]> wrote:
> I'm using event delegation (http://www.danwebb.net/2008/2/8/event-
> delegation-made-easy-in-jquery) to capture events that have bubbled up
> to the #nav element. It might even make sense to capture clicks that
> bubble up to the document object. I want to use the .is function to
> figure out what the user clicked on. Can I pass selector expressions
> like "div div#nav a.exit" to the .is function?
>
> On Feb 20, 2:18 pm, Hamish Campbell <[EMAIL PROTECTED]> wrote:
>
>
>
> > Your jQuery is a bit faulty. You've only applied the event to the div
> > called nav, not to the links. Also, you don't need to use event.target
> > - the function will fire with this == the bound element.
>
> > Try:
>
> > <div id="nav">
> >     <a href="/exit/" class="exit">Exit</a>
> >     <a href="/open/" class="open">Open</a>
> > </div>
>
> > <script type="text/javascript" charset="utf-8">
> > jQuery(document).ready(function(){
> >     jQuery('#nav a').bind('click', function() {
> >         alert(jQuery(this).is('.exit'));
> >         return false;
> >     });});
>
> > </script>
>
> > On Feb 21, 7:10 am, hartshorne <[EMAIL PROTECTED]> wrote:
>
> > > Hi, I'm new to jQuery, and found something unexpected. With something
> > > like this:
>
> > > <div id="nav">
> > >     <a href="/exit/" class="exit">Exit</a>
> > >     <a href="/open/" class="open">Open</a>
> > > </div>
> > > <script type="text/javascript" charset="utf-8">
> > > jQuery('#nav').bind('click', function() {
> > >     alert(jQuery(event.target).is('#nav .exit'));
> > >     event.preventDefault();});
>
> > > </script>
>
> > > I expect .is('#nav .exit') to return true when the event is fired from
> > > the Exit link, and false otherwise. Instead it always returns true.
> > > What am I missing?
>
> > > Thanks!- Hide quoted text -
>
> - Show quoted text -

Reply via email to