2013-04-30 85 views
1

我想隱藏圖片庫中的圖片,當有人使用右鍵點擊嘗試下載圖片時。隱藏的物體使用jQuery是很容易的,當用戶按正常左鍵點擊,只需添加一個簡單的事件用jquery點擊右鍵隱藏圖片

$("document").ready(function(){ 
    $("img").click(function(event){ 
     $(this).hide("fast"); 
    }); 
}); 

但我怎麼能檢測鼠標的右鍵激活event.preventDefault();並設置$(this).hide行動在圖像...

+1

這已被問到之前 - http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery。你知道用戶可以禁用JS,對吧? – Terry 2013-04-30 21:33:43

+0

@Terry甚至不需要禁用JS,你可以做到以下所有:去工具>查看源文件,Ctrl + U,F12的Firebug/DevTools等等''contextmenu'只是一個傻瓜的障礙:) – RaphaelDDL 2013-04-30 21:40:44

回答

1

contextmenu觸發事件上點擊右鍵,這樣平時的快捷方式是使用事件:

$(document).ready(function(){ 
    $("img").on('contextmenu', function(event){ 
     $(this).hide("fast"); 
    }); 
}); 

另一種方法是使用鼠標按下事件,並檢查什麼按鈕wa Š使用:

$("img").on('mousedown', function(event){ 
    if (event.which === 3) 
     $(this).hide("fast"); 
}); 

在我的瀏覽器(Chrome),後者的解決方案不以click事件,只有mousedown事件工作

2
$("document").ready(function(){ 
    $("img").click(function(event){ 
     if(event.which === 3) { 
      $(this).hide("fast"); 
     } 
    }); 
}); 
+0

這並不' t似乎可以在Chrome或Firefox中使用。 http://jsbin.com/emotax/2/edit – CraigTeegarden 2013-04-30 21:37:29

+0

@ c4p - 不,它不是! – adeneo 2013-04-30 21:38:31

0

您可以通過使用禁用上下文菜單contextmenu事件:

$(document).on("contextmenu","img", function(e){ 
    //hide image ,show alert , etc.. 
    return false; 
}); 

無論如何,這不會阻止用戶能夠保存在page.they圖像依然能看到圖像的URL通過並直接獲取它們,或從瀏覽器緩存中獲取它們。