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

$(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(); }); 

你好再次Stackoverflow!

对于我当前的项目,我想在每个链接上禁用鼠标右键和中键。 当用鼠标左键单击时,如果链接不包含target="_blank" ,我需要调用一个使用AJAX加载该页面的函数。 (function loadpage() )。

虽然鼠标中键仍然打开一个新选项卡,但这段代码工作正常。 我该如何解决这个问题?

提前致谢!

在该事件处理程序中,调用

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

或:使用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);