禁用图像上的右键单击

Y. *_* A. 1 javascript jquery

我目前正在使用此脚本来防止右键单击我的网站之一。我对代码进行了不同的调整,因为我希望它能够防止仅右键单击图像(右键单击它无处不在)。

任何想法?

在此先感谢您的帮助

//Disable right mouse click Script

var message = "Stop trying stealing pics! :P";

///////////////////////////////////
function clickIE4() {
  if (event.button == 2) {
    alert(message);
    return false;
  }
}

function clickNS4(e) {
  if (document.layers || document.getElementById && !document.all) {
    if (e.which == 2 || e.which == 3) {
      alert(message);
      return false;
    }
  }
}

if (document.layers) {
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown = clickNS4;
} else if (document.all && !document.getElementById) {
  document.onmousedown = clickIE4;
}

document.oncontextmenu = new Function("alert(message);return false")
Run Code Online (Sandbox Code Playgroud)

Sil*_*fer 6

使用contextmenu事件:

$(document).ready(function() {
     $("img").on("contextmenu",function(){
        return false;
     }); 
 });
Run Code Online (Sandbox Code Playgroud)
img{
  width: 50px;
  height: 50px;
  border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded">
Run Code Online (Sandbox Code Playgroud)

  • 单击并按住,拖动到新选项卡 (9认同)