jQuery not().bind('click'); doesn't work
jQuery('div').n开发者_Python百科ot("#supcont").bind('click', function() {
jQuery("#supcont").slideUp("slow");
});
On:
jQuery("#supcont").click();
Acts like #supcont
is not exluded. Why?
It should work fine. The only reason I can think of that it may not, is that your #supcont
element is a descendant of a div
, and as the click event will bubble up, the div
will receive it. To prevent that happening, you can use the stopPropagation
method of the event object:
jQuery('div').not("#supcont").bind('click', function(e) {
e.stopPropagation();
jQuery("#supcont").slideUp("slow");
});
Update (see comments)
Actually, the above won't help you if #supcont
is a descendant of another div
, you will need to capture the event on #supcont
and stop it there. So bind a click event handler to #supcont
too:
jQuery("#supcont").click(function(e) {
e.stopPropagation();
});
Instead of adding an event to each <div>
tag on the page , you should add a single event handler on a whole document
. You should examine the ways to use event delegation and "bubbling".
- http://www.sitepoint.com/javascript-event-delegation-is-easier-than-you-think/
- http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-javascript-event-delegation-in-4-minutes/
Another way to write this would be:
$('div').click(function() {
if(!$(this).is('#supcont'))
$('#supcont').slideUp("slow");
});
$('#supcont').click()
精彩评论