简体   繁体   English

我只想启用右键点击图片

[英]I want to enable right click on image only

I am using code to block right click on my blog. 我正在使用代码来阻止右键单击我的博客。 It's work for may website and disable ctrl+v ctrl+c-right click f12 on my webpage. 它适用于May网站,并在我的网页上禁用ctrl + v ctrl + c右键单击f12。 But I want to enable right click on image. 但是我要启用右键单击图像。 I don't know how to do that. 我不知道该怎么做。 But Please Help me. 但请帮助我。

var isCtrl = false;
document.onkeyup = function(e) {
  if (e.which == 17)
    isCtrl = false;
}

document.onkeydown = function(e) {
  if (e.which == 123)
    isCtrl = true;

  if (((e.which == 85) || (e.which == 65) || (e.which == 88) || (e.which == 67) || (e.which == 86) || (e.which == 2) || (e.which == 3) || (e.which == 123) || (e.which == 83)) && isCtrl == true) {
    alert('This is Function Disabled');
    return false;
  }
}

// right click code
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if (navigator.appName == "Netscape")
  document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);

function mischandler() {
  alert('This is Function Disabled');
  return false;
}

function mousehandler(e) {
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
  if ((eventbutton == 2) || (eventbutton == 3)) return false;
}

document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;

//select content code disable  alok goyal
function killCopy(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function("return false")
if (window.sidebar) {
  document.onmousedown = killCopy
  document.onclick = reEnable
}

I have updated code which checks if selected element is image. 我更新了代码,该代码检查所选元素是否为图像。 Using event object we can check the nature of selected element 使用事件对象,我们可以检查所选元素的性质

function mischandler(e) { //change
    if(e.currentTarget.getAttribute("src")==null) //change
    {
        alert('This is Function Disabled');
        return false;
    }

}

function mousehandler(e) {
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
  if (((eventbutton == 2) || (eventbutton == 3)) && e.currentTarget.getAttribute("src")==null) //change
    return false;
}

It's easy using event.target we can enable/disable right-click when mouse enters/leaves a particular image section on your web page. 使用event.target很容易,当鼠标进入/离开网页上的特定图像部分时,我们可以启用/禁用右键单击。

Just see a demo of the same below 请看下面的演示

 var isCtrl = false; document.onkeyup = function(e) { if (e.which == 17) isCtrl = false; } document.onkeydown = function(e) { if (e.which == 123) isCtrl = true; if (((e.which == 85) || (e.which == 65) || (e.which == 88) || (e.which == 67) || (e.which == 86) || (e.which == 2) || (e.which == 3) || (e.which == 123) || (e.which == 83)) && isCtrl == true) { alert('This is Function Disabled'); return false; } } // right click code var isNS = (navigator.appName == "Netscape") ? 1 : 0; if (navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP); function mischandler(e) { let target = $(e.target); if(!target.is('#img')) { alert('This is Function Disabled'); return false; } } function mousehandler(e) { var myevent = (isNS) ? e : event; var eventbutton = (isNS) ? myevent.which : myevent.button; if ((eventbutton == 2) || (eventbutton == 3)) return false; } document.oncontextmenu = mischandler; document.onmousedown = mousehandler; document.onmouseup = mousehandler; $('#img').on('mouseenter', function(e) { mischandler(e); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src='https://www.w3schools.com/html/pic_mountain.jpg' id='img' alt='mountain.jpg' width='200' height='200'/> 

Hope, this works fine to you. 希望这对您有效。 :) :) :) :)

With jQuery I think a rather simple solution would be: 使用jQuery,我认为一个比较简单的解决方案是:

 $(document).on( "contextmenu", function(event) {
//if image allow normal browser behaviou
        if(event.target.tagName.toLowerCase() === 'img'){
            return true;
        }else{
//if any other event target prevent default
            event.preventDefault;
            alert("no right click");
            return false;
        }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM