Disable right click on specific <div> or class=“” [closed]

匿名 (未验证) 提交于 2019-12-03 02:23:02

问题:

This is my sample code:

html

<div>     This is the Phone and NO ONE SHOULD RIGHT CLICK THIS! >:) </br>         <img 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;"> 

js

$('img').bind('contextmenu', function(e){     alert("This Logo is protected");return false; }); 

fiddle

I want no one to be able to right click the 1st picture (cellphone) but other than that(keyboard) should be able to right click.

PS: I know this can be overriden by browsers but its okay :)

回答1:

Came up with a solution.

$('.tlClogo').bind('contextmenu', function(e) {     return false; });  

Fiddle: http://jsfiddle.net/79k52rvu/4/

EDIT 1: Only the first one is now NOT right-clickable!

<html>     <body>         <div>         This is the Phone and NO ONE SHOULD RIGHT CLICK THIS! >:) </br>         <img 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;">          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>         <script>             $('.tlClogo').bind('contextmenu', function(e) {                 return false;             });          </script>     </body> </html> 

EDIT 2: Provided an HTML-doc



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!