开发者

Hide when clicked on document except when clicked on a div

开发者 https://www.devze.com 2023-01-06 01:39 出处:网络
I want to hide a div when clicked on a document but I dont want to hide that div when s开发者_StackOverflowomeone clicks in it or clicks a link or a button in it. Also, I have some links inside that d

I want to hide a div when clicked on a document but I dont want to hide that div when s开发者_StackOverflowomeone clicks in it or clicks a link or a button in it. Also, I have some links inside that div set to prevent the click action (return false;) and send a ajax request.

I tried:

$(document).click(function(e) {
      $('#bubble').hide();
});

$('#bubble').click(function(e) {
    return false;
});

It works fine but the links and buttons under the #bubble doesn't work.


Instead of return false; use event.stopPropagation() like this:

$('#bubble').click(function(e) {
  e.stopPropagation();
});

This stops the click event from bubbling up to document like you want, but won't kill the event dead in its tracks like return false; will. All you need to do in this case is prevent the default bubbling behavior, this does only that :)

0

精彩评论

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