Disable right click except some elements

I use this code to disable the right click on my page:

$(document).ready(function() {
    $(document).bind("contextmenu",function(e) {
       return false;
    });
}); 

How can I add some kind of exception, say, when you right-click on an image, the context menu should not be disabled.

Maybe something with stopPropagation, but can't figure it out.

thank

+4
source share
2 answers

with event.target:

$(document).ready(function() {
  $(document).bind("contextmenu", function(e) {
     if(!$(e.target).is('img')){
        return false;
     }
  });
});
+4
source

Try the following:

  <script type="text/javascript">
    $(function () { 
      $('#btnClick').bind('contextmenu',function(e){
        e.preventDefault();
        alert('Right Click is not allowed');
      }); 

    });
  </script>



 <input type="text" /><br>
    <input type="checkbox" />

  <button id="btnClick">Click</button>

http://jsfiddle.net/NLJ6t/1/

0
source

Source: https://habr.com/ru/post/1544880/


All Articles