禁用上下文菜单和右键菜单 [英] Disable contextmenu and rightclick menu

查看:147
本文介绍了禁用上下文菜单和右键菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello once again Stackoverflow!

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

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?

提前谢谢!

推荐答案

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

Within that event handler, call

e.preventDefault():

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

或: 通过使用JAVASCRIPT禁用鼠标滚轮事件:

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

这篇关于禁用上下文菜单和右键菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆