I have a content with with a width of 720px in the middle 开发者_如何学Goof a body. I want, if people press doubleclick outside of that content to do function using jQuery.
I know how I could do the if double click on the body
but that includes the content aswell so I'd liked to know how can I exclude some div's and classes.
Thanks alot
Simply stop the propagation of the dblclick event from the content to the rest of the document:
$('#content').dblclick(function(event) {
event.stopPropagation();
});
With this, any double click event on the #content element will not be received by any of its parent.
So the following handler will not receive dbclick events for double clicks on the content element:
$(document).dblclick(function() {
// ....
});
Try it here: http://jsfiddle.net/E4tZQ/
See event.stopPropagation()
documentation.
You need both a way to test for the double click, and to stop propagation when the content div is clicked:
http://jsfiddle.net/STchX/
You can set another doubleclick handler to the container of your content (720px) which has event.preventDefault() inside and returns false;
$('#myContentDiv').bind('dblclick', function(event) {
event.preventDefault();
return false;
});
event.stopPropagation() works too.
精彩评论