开发者

jQuery event problem when implementing contextmenu?

开发者 https://www.devze.com 2022-12-16 21:54 出处:网络
When the contextmenu is shown, if the user clicks out of the contextmenu, should hide it. How to implement this functionality?

When the contextmenu is shown,

if the user clicks out of the contextmenu,

should hide it.

How to implement this functionality?

Is it possible to listen for click_out,and when it's detected,hide contextmen开发者_运维问答u and clear that listener?


You can bind to the body.click to hide it. Any click event on any other element bubbles up to the body eventually:

$('body').click(function() {
   $('#menu').hide();
});

Above example is assuming your custom menu has the ID of 'menu'. Replace as needed.

Depending on how your menu works (if you have nested menus you can click to open) you might want to bind something to the clicks inside it to stop the event using e.stopPropagation();

To clear the listener you can do:

$('body').click(function() {
   $('#menu').hide();
   $(this).unbind('click');
});


You could add an onclick handler to the document and then check if the event target is the menu or not. If the click is on the menu, do nothing, if not hide it.

jQuery(document).click(function(event) {
  if(event.target==$('context-menu'){
    $('context-menu').hide();
    $(this).unbind('click');
  }
}):


Here is a snippet of some working code:

document.onclick = Tree.hideContext;

    Tree = {
        hideContext: function() {
            $("#context").hide();
        }
    }
0

精彩评论

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

关注公众号