简体   繁体   English

禁用上下文菜单和右键菜单

[英]Disable contextmenu and rightclick menu

$(document).on('mousedown', 'a', function(event){ 
    event.preventDefault();

    if(event.which == 1){
        if($(this).attr('target') != '_blank'){
            loadpage($(this).attr('href'));
        }
    }
}).on('contextmenu', 'a', function(event){
    event.preventDefault();
});

Hello once again Stackoverflow! 您好,Stackoverflow再一次!

For my current project I want to disable the right and middle mouse button on every link. 对于我当前的项目,我想在每个链接上禁用鼠标右键和鼠标中键。 And when clicked on with the left mouse button, if the link doesn't contain target="_blank" , I need to call a function that loads that page using AJAX. 当用鼠标左键单击时,如果链接不包含target="_blank" ,则需要调用一个使用AJAX加载该页面的函数。 (function loadpage() ). (函数loadpage() )。

This piece of code works decently, although the middle mouse button still opens a new tab. 尽管鼠标中键仍会打开一个新选项卡,但这段代码运行得不错。 How do I solve this? 我该如何解决?

Thanks in advance! 提前致谢!

Within that event handler, call 在该事件处理程序中,调用

e.preventDefault():

$("#foo").on('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
   }
});

or: Disable mouse wheel event by using JAVASCRIPT : 或:使用JAVASCRIPT禁用鼠标滚轮事件:

In IE:

document.attachEvent('onmousewheel', function(e){
     if (!e) var e = window.event;
     e.returnValue = false;
     e.cancelBubble = true;
     return false;
}, false);
In Safari:

document.addEventListener('mousewheel', function(e){
    e.stopPropagation();
    e.preventDefault();
    e.cancelBubble = false;
    return false;
}, false);
In Opera:

document.attachEvent('mousewheel', function(e){
    if (!e) var e = window.event;
    e.returnValue = false;
    e.cancelBubble = true;
    return false;
}, false);
In Firefox:

document.addEventListener('DOMMouseScroll', function(e){
    e.stopPropagation();
    e.preventDefault();
    e.cancelBubble = false;
    return false;
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM