[英]jQuery onClick on Custom Context Menu
我有一個自定義右鍵單擊上下文菜單,右鍵單擊頁面時顯示。 在右鍵單擊事件后,如果用戶單擊該頁面,則隱藏上下文菜單。 我需要解決的問題是,如果用戶單擊上下文菜單(選擇下拉列表),則jQuery onClick事件將隱藏上下文菜單。 有沒有辦法識別被點擊的元素的div,以便我可以決定是否從那里隱藏菜單?
$('body').on('contextmenu', options.contextMenu.graphName, function (event) {
showContextMenu(event);
});
$(document).bind('click', function (event) {
//if(event.targetDiv.id != '#graphMenu') <- Is something like this possible?
$('#graphMenu').hide();
});
有一些方法可以實現這一點,但我所知道的最好的方法是在菜單懸停時添加一個標志,並在鼠標離開菜單時將其刪除:
$('#graphMenu')
.mouseenter(function(e) {
$(this).data('hovered', true);
})
.mouseleave(function(e) {
$(this).data('hovered', false);
});
$('body').on('contextmenu', options.contextMenu.graphName, function (event) {
showContextMenu(event);
});
$(document).bind('click', function (event) {
if (!$('#graphMenu').data('hovered'))
$('#graphMenu').hide();
});
嘗試:
$('body').on('contextmenu', options.contextMenu.graphName, function (event) {
showContextMenu(event);
});
$(document).bind('click', function (event) {
if(event.target.id != '#graphMenu') {
$('#graphMenu').hide();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.