2010-06-02 31 views
10
document.addEventListener('contextmenu', function (e) { 
    e.preventDefault() 
    e.stopPropagation() 
    e.returnValue = false 
    e.cancleBubble = true 
}) 

沒辦法?如何在這種情況下重新啓用上下文菜單?

編輯:document.oncontextmenu = null不起作用。

P.S.我不能擁有監聽器功能的引用,因爲我不是阻止上下文菜單的站點的所有者。

+0

什麼你的意思是「道德」? – 2010-06-02 22:20:14

+7

在我看來,故意禁用上下文菜單的網站應該禁止或不運行腳本。 (我使用NoScript。)我經常通過右鍵單擊並選擇「後退」或「前進」進行導航。沒有什麼不合乎想要的上下文菜單工作。站點作者首先禁用它是愚蠢的。 – JYelton 2010-06-02 22:23:55

+0

你有沒有試過'document.oncontextmenu = null;'? – 2010-06-02 22:44:36

回答

4

如果你真的絕望,嘗試添加此addEventListener被調用之前。它適用於FF和Chrome。我沒有檢查其他東西。

document.superListener = document.addEventListener; 
document.addEventListener = function(type, listener, useCapture){ 
    if(type != 'contextmenu') 
     document.superListener(type, listener, !!useCapture); 
}; 

它可能不是做事情的最好方法,但它應該在你的具體的例子:)

+0

謝謝你的回答。看起來不可能重新啓用上下文菜單「之後」調用... – 2010-06-13 16:08:29

16

我用我在這種情況下,書籤來完成這項工作:

javascript:(function(w){ 
    var arr = ['contextmenu','copy','cut','paste','mousedown','mouseup','beforeunload','beforeprint']; 
    for(var i = 0, x; x = arr[i]; i++){ 
     if(w['on' + x])w['on' + x] = null; 
     w.addEventListener(x, function(e){e.stopPropagation()}, true); 
    }; 
    for(var j = 0, f; f = w.frames[j]; j++){try{arguments.callee(f)}catch(e){}}})(window); 
+0

這一個工作正常。只需將選項限制爲「contextmenu」。 – ThiagoPonte 2016-04-27 12:36:19

+0

工作得很好,謝謝 – 2017-03-28 10:50:29

+0

我該如何投票10次?謝謝! – tyron 2017-05-17 03:27:32