我正在覆盖上下文菜单(仅当特定元素被右键单击时)。我这样做是通过绑定contextmenu然后执行event.preventDefault()。
我遇到的问题是我在event.preventDefault()之后运行了一些逻辑,如果某个特定的senario是真的,我想显示原始的contextmenu而不是自定义的。
所以我的问题是,在我已经执行了preventDefault后,如何显示默认上下文菜单。
$(document).bind("contextmenu", function (event) {
if(event.shiftKey || event.ctrlKey) {
return;
}
attr = event.target.attributes
if(!attr.length){ //if we don't have any attributes we automatically know this isn't the data-role="taskName" so return
return;
}
val = event.target.attributes[0].nodeValue;
if(!val.length){
return; //we do have attributes but if that attribute doesn't equal taskName then return
}
switch(val){
case 'taskName':
//code here
break;
case 'individualTaskNote_contexTrigger':
//read above
break;
case 'description':
//read above
//if we are currently in edit mode we don't allow right clicks to go into edit mode
if(helper_bool_descriptionAlreadyInEditMode){
return;
}
break;
default:
return;
break;
}
// Avoid the real one
event.preventDefault();
switch(val){
case 'taskName':
//irreverent code here
break;
/*********************/
/*********************/
/*********************/
case 'individualTaskNote_contexTrigger':
var $contextMenu = $customMenuNotesComment;
var $parentNode = $(event.target.parentNode);
if($parentNode.hasClass('alert')){
//correct element
//console.log('golden');
}else{
if($parentNode.attr('id') == 'notes_theNotes'){
//idk what to do
//console.log('here');
$temp = $(event.target);
//console.log($temp);
if($temp.hasClass('alert')){
$parentNode = $temp;
//console.log('yess');
}else{
$parentNode = $(event.target).parents('alert');
//console.log('find parent');
}
}else{
$parentNode = $parentNode.parents('.alert');
//console.log('else');
}
}
helper_int_contexMenu_taskIDRightClicked = $parentNode.attr('data-note-id');
if(typeof helper_obj_notesCommentsAlreadyInEditMode[helper_int_contexMenu_taskIDRightClicked] != 'undefined' && helper_obj_notesCommentsAlreadyInEditMode[helper_int_contexMenu_taskIDRightClicked]){
//event.contextmenu();
/* HERE IS WHERE i WANT TO SHOW THE ORIGINAL CONTEXT MENU, IF THIS IS TRUE SHOW DEFAULT CONTEXT MENU */
return; //if we are in edit mode let's no longer continue!
}
$parentNode.addClass('taskRename-highlight');
// Show contextmenu
if ($parentNode.hasClass("highlight-task")){ //if the currently right clicked task is currently highlighted
//this task is currently highlighted, so we want to show our display notification menu
$contextMenu.find('li[data-action="second"]').show();
}
if($parentNode.attr('data-created-by') == loggedInUser){
$contextMenu.find('li[data-action="third"]').show();
}
break;
/*********************/
/*********************/
/*********************/
case 'description':
//irreverent code here
break;
}
$contextMenu.finish().toggle(350).
// In the right position (the mouse)
css({
top: (event.pageY + 1) + "px",
left: (event.pageX - 2) + "px"
});
});
正如你所看到的,在剪辑的底部,我有一个注释,我想显示默认的上下文菜单。我想也许event.contextmenu()会起作用,但事实并非如此。
任何人都有任何建议 (我知道我可以将event.preventDefault()模式下方的代码剪切注释,这样可以工作,但我希望尽快执行event.preventDefault(),这是最后的手段。)< / p>
ANSWER
根据说明你不能的评论,答案是将event.preventDefault()移动到第二个switch语句下面。这样当我们返回时它仍然显示默认值,因为我们还没有阻止它。