Javascript 禁用右键单击特定的 <div> 或 class=""
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28062979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable right click on specific <div> or class=""
提问by EngrAbbas
This is my sample code:
这是我的示例代码:
html
html
<div>
This is the Phone and NO ONE SHOULD RIGHT CLICK THIS! >:) </br>
<img class="tlClogo" src="http://i.imgur.com/0atiS5C.jpg" style="height: 120px; width:120px;">
</div></br></br></br></br>
And this is the Keyboard, ofcourse yo can right click this :)</br>
<img src="http://i.imgur.com/xkrKz1X.jpg" style="height: 120px; width:120px;">
js
js
$('img').bind('contextmenu', function(e){
alert("This Logo is protected");return false;
});
I want no one to be able to right click the 1st picture (cellphone) but other than that(keyboard) should be able to right click.
我希望没有人能够右键单击第一张图片(手机),但除此之外(键盘)应该能够右键单击。
PS: I know this can be overriden by browsers but its okay :)
PS:我知道这可以被浏览器覆盖,但没关系:)
回答by Gustaf Gunér
Came up with a solution.
想出了一个解决方案。
$('.tlClogo').bind('contextmenu', function(e) {
return false;
});
Fiddle: http://jsfiddle.net/79k52rvu/4/
小提琴:http: //jsfiddle.net/79k52rvu/4/
EDIT 1: Only the first one is now NOT right-clickable!
编辑 1:只有第一个现在不可右键单击!
<html>
<body>
<div>
This is the Phone and NO ONE SHOULD RIGHT CLICK THIS! >:) </br>
<img class="tlClogo" src="http://i.imgur.com/0atiS5C.jpg" style="height: 120px; width:120px;">
</div>
</br></br></br></br>
And this is the Keyboard, ofcourse yo can right click this :)</br>
<img src="http://i.imgur.com/xkrKz1X.jpg" style="height: 120px; width:120px;">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$('.tlClogo').bind('contextmenu', function(e) {
return false;
});
</script>
</body>
</html>
EDIT 2: Provided an HTML-doc
编辑 2:提供了 HTML 文档

