禁用右键单击特定的<div>或class =“”</div>

时间:2015-01-21 08:51:59

标签: javascript jquery

这是我的示例代码:

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

我希望没有人能够右键单击第一张照片(手机),但除此之外(键盘)应该能够右键单击。

PS:我知道这可以被浏览器覆盖,但没关系:)

1 个答案:

答案 0 :(得分:14)

想出了解决方案。

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

小提琴:http://jsfiddle.net/79k52rvu/4/

编辑1:现在只有第一个不可右键点击!

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

编辑2:提供HTML-doc