开发者

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

开发者 https://www.devze.com 2022-12-25 01:24 出处:网络
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 s

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

精彩评论

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