jQuery 如何使用jquery防止右键单击选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5618109/
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
How to prevent Right Click option using jquery
提问by mymotherland
Is it possible to prevent RIGHT CLICK option for IMAGES which we use in web page.
是否可以阻止我们在网页中使用的图像的右键单击选项。
回答by Peeter
$(document).ready(function() {
$("img").on("contextmenu",function(){
return false;
});
});
Working example: http://jsfiddle.net/vak9exyk/
工作示例:http: //jsfiddle.net/vak9exyk/
回答by Abdul Kader
I think this should help. Trick is to bind the contextmenu event.
我认为这应该有所帮助。技巧是绑定上下文菜单事件。
<script type="text/javascript" language="javascript">
$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
});
</script>
回答by Amit mishra
<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false" >
<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false" >
Set these attributes in your selected tag
在您选择的标签中设置这些属性
See here Working Example -https://codepen.io/Developer_Amit/pen/drYMMv
请参阅此处的工作示例 - https://codepen.io/Developer_Amit/pen/drYMMv
No Need JQuery(like)
不需要JQuery(喜欢)
回答by anupam
$(document).ready(function() {
$(document)[0].oncontextmenu = function() { return false; }
$(document).mousedown(function(e) {
if( e.button == 2 ) {
alert('Sorry, this functionality is disabled!');
return false;
} else {
return true;
}
});
});
If you want to disable it only on image click the instead of $(document).mousedown
use $("#yourimage").mousedown
如果您只想在图像上禁用它,请单击而不是$(document).mousedown
使用$("#yourimage").mousedown
回答by JoyGuru
The following code will disable mouse right click from full page.
以下代码将禁用从整页鼠标右键单击。
$(document).ready(function () {
$("body").on("contextmenu",function(e){
return false;
});
});
The full tutorial and working demo can be found from here - Disable mouse right click using jQuery
可以从这里找到完整的教程和工作演示 -使用 jQuery 禁用鼠标右键单击
回答by Payal Mittal
Try this:
尝试这个:
$(document).bind("contextmenu",function(e){
return false;
});
回答by Black
Here is a working example, the red links can't be right clicked anymore.
这是一个工作示例,无法再右键单击红色链接。
$("ul.someLinks1 a").each(function(i, obj) {
$(obj).on("contextmenu",function(){
return false;
});
$(obj).css("color", "red");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="someLinks1">
<li><a href="www.google.de">google</a></li>
<li><a href="www.stackoverflow.de">stackoverflow</a></li>
<li><a href="www.test.de">test</a></li>
</ul>
<ul class="someLinks2">
<li><a href="www.foobar.de">foobar</a></li>
<li><a href="www.foo.de">foo</a></li>
<li><a href="www.bar.de">bar</a></li>
</ul>
回答by Rohit Agrohia
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
</script>
</head>
<body>
<p>Right click is disabled on this page.</p>
</body>
</html>
回答by Gaurang P
Method 1:
方法一:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
</script>
Method 2:
方法二:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
e.preventDefault();
});
});
</script>
回答by Bharath Kumaar
Here i have found some useful link, with live working example.
在这里,我找到了一些有用的链接,带有现场工作示例。
I have tried its working fine.
我试过它的工作正常。
How to prevent Right Click option using jquery
$(document).bind("contextmenu", function (e) {
e.preventDefault();
alert("Right Click is Disabled");
});