开发者

Box disappear, when clicking elsewhere on the document? (jQuery)

I'm showing a hidden box with jQuery on link-click. Now the box disappears when clicking the link again, but how to make it so it 开发者_开发问答kinda "loses focus" and hides. SO, when user click somewhere on document (but not the box itself), it disappears.

Suggestions?

Martti Laine


A click on the box will bubble to the document, so catching a click there will always close it. To prevent this, a click inside the box will be caught/stopped, a click outside won't, causing it to bubble up and close. All the code you need to do this is:

$(document).click(function() {
  $("#myBox").hide();
});

$("#myBox").click(function(e) {
  e.stopPropogation();
});

...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜