如何在网页中禁用图片的右键点击功能?
如何在网页中禁用图片的右键点击功能?
$(document).ready(function () {
$(document).ready(function() { $("img").on('contextmenu', function (e) {
e.preventDefault();
});
});
你还可以使用.contextmenu()快捷方法,例如:
$(document).ready(function() {
$("#logo").contextmenu(function(e){
return false;
});
});
body {
background-color: #FFF;
}
#logo {
background: url(http://stackoverflow.com/favicon.ico) no-repeat;
width: 182px;
height: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Try right-click on the logo.
<div id="logo"></div>
<!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>
$(document).mousedown(function(e) {
if( e.button == 2 ) {
e.preventDefault();
return false;
}
});
如果您想禁用图像的下载/保存,脚本无法阻止这一点。您可能会在服务器配置级别上更好地做到这一点(例如,在Apache上修改您的.htaccess
)。
尝试在ServerFault上提问。