开发者

Question on jQuery event [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to detect a click outside an element?

I have an element on page say "myDiv" Using jQuery, how do I fire an event which would track a "tap" on any other area apart from "myDiv"

I am sa开发者_如何学运维ying "tap" as I want to test it on iPad...You can even assume a normal "click" event instead of "tap"...Point is it should be "outside" of "myDiv"


Try binding a click handler to the body element and another click handler to myDiv which stops propagation of the click event. Here's a jsFiddle demonstrating the concept.


An event on the tag should do it.

$("html").click(function(e) {
    alert("Test");
});

Example: http://jsfiddle.net/HenryGarle/HWApM/

Depending on your needs you could use the not selector:

$("div:not(div#winner)").live('click', function(e) {
    alert("Test");
});

Example: http://jsfiddle.net/HenryGarle/HWApM/1/

Or if its a nested div:

$("html").click(function(e) {
    alert("Test");
});

$("#winner").live('click', function(e) {
     e.stopPropagation(); 
});

Example: http://jsfiddle.net/HenryGarle/HWApM/2/


With the following you can register what you want to do when you click any area

$(document).click(function() {
  //your code here
});

And after that you just remove the callback from your div.

$("#myDiv").unbind("click");

This should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜