2013-07-02 41 views
1

现在,我使用此代码禁用右键点击:如何禁用某些HTML页面上点击右键有一些例外

$("body") 
     .attr('ondragstart', 'return false') 
     .attr('onselectstart', 'return false'); 

    var message='Right click is disabled on this page!'; 
    function clickIE4() { 
     if (event.button==2) { 
     alert(message); 
     return false; 
     } 
    } 

    function clickNS4(e) { 
     if (document.layers||document.getElementById&&!document.all) { 
     if (e.which==2||e.which==3) { 
      alert(message); 
      return false; 
     } 
     } 
    } 

    if (document.layers) { 
     document.captureEvents(Event.MOUSEDOWN); 
     document.onmousedown=clickNS4; 
    } else if (document.all&&!document.getElementById) { 
     document.onmousedown=clickIE4; 
    } 
    document.oncontextmenu = new Function('alert(message); return false'); 

我自己的现在,我已经实现了上下文菜单,我显示它当点击某个a元素与类.context

我使用http://medialize.github.io/jQuery-contextMenu/demo.html

所以我想禁用除一个与.context类的所有右键单击上下文菜单

+3

您必须从某处已经没有东西像10多年来一直更新 – erikkallen

+3

实在是禁用右键点击任何万无一失的方法复制粘贴此。任何人都可以查看您的来源并随心所欲。甚至在Firebug或其他程序中打开它并实时禁用脚本。除了激怒用户之外,它不会取得多大成就。 –

+1

我同意,从可用性的角度来看,这是一个非常糟糕的做法。除了可能是恼人的用户之外,你绝对没有什么收获。 – luk2302

回答

1

你可以尝试这样的:

$('body *:not(.context)').on('contextmenu', function (evt) { 
    evt.preventDefault(); 
}); 

编辑:这似乎效果更好:

$(document).on('contextmenu', function (evt) { 
    if (!$(evt.target).is('.context')) { 
     evt.preventDefault(); 
    } 
}); 
0

使用这个片段:

var message="Right click is disabled on this page!"; 
jQuery("*").on("contextmenu", function(e) { 
    e.stopPropagation(); 
    if ($(this).hasClass("context")) { 
     alert("Opening custom right-click menu."); 
    } 
    else { 
     alert(message); 
    } 
    return false; 
}); 

See a demo here.

相关问题