我想只启用右键单击图像

时间:2017-08-24 17:42:46

标签: javascript jquery html5 jquery-plugins

我正在使用代码阻止右键点击我的博客。它可以为may网站工作并禁用ctrl + v ctrl + c-右键单击我的网页上的f12。但我想启用右键单击图像。我不知道该怎么做。但请帮助我。

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
}

3 个答案:

答案 0 :(得分:0)

我更新了代码,检查所选元素是否为图像。使用事件对象,我们可以检查所选元素的性质

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;
}

答案 1 :(得分:0)

使用event.target可以轻松使用启用/禁用右键单击鼠标进入/离开网页上的特定图片部分

只需看下面相同的演示



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'/>
&#13;
&#13;
&#13;

希望,这对你很好。 :):)

答案 2 :(得分:0)

使用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;
        }
    })