Disable contextmenu and rightclick menu

不打扰是莪最后的温柔 提交于 2020-01-06 03:52:04

问题


$(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!

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. (function 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!


回答1:


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 :

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);


来源:https://stackoverflow.com/questions/19985320/disable-contextmenu-and-rightclick-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!