I have a javascript that prevents right click on an HTML page:
document.addEventListener("contextmenu", function(e){
e.preventDefault();
}, false);
I have a <input>
tag on that same page with the name "Link" that I want the right click to happen on.
How can I achieve that?
You can check and test e.target
of the event:
document.addEventListener("contextmenu", function(e){
if (e.target.tagName.toLowerCase() === 'input' && e.target.name === 'Link') {
return; //pass
}
e.preventDefault(); // prevent others
}, false);
Put an if
statement inside your event listener:
document.addEventListener("contextmenu", function(e){
if (e.target.name !== "Link") {
e.preventDefault();
}
}, false);
So it basically says: when the target element does not have a name of Link
prevent the right click.
<div> This is the Phone and NO ONE SHOULD RIGHT CLICK THIS! >:) </br> <img oncontextmenu="return false;" class="tlClogo" src="http://i.imgur.com/0atiS5C.jpg" style="height: 120px; width:120px;"> </div> </br></br></br></br> And this is the Keyboard, ofcourse yo can right click this :)</br> <img src="http://i.imgur.com/xkrKz1X.jpg" style="height: 120px; width:120px;">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.