How to prevent Right Click option using jquery

前端 未结 12 2112
心在旅途
心在旅途 2020-12-02 11:23

Is it possible to prevent RIGHT CLICK option for IMAGES which we use in web page.

相关标签:
12条回答
  • 2020-12-02 11:39

    Try this:

    $(document).bind("contextmenu",function(e){
        return false;
    });
    
    0 讨论(0)
  • 2020-12-02 11:40

    <body oncontextmenu="return false" onselectstart="return false" ondragstart="return false" >

    Set these attributes in your selected tag

    See here Working Example - https://codepen.io/Developer_Amit/pen/drYMMv

    No Need JQuery (like)

    0 讨论(0)
  • 2020-12-02 11:41

    Here is a working example, the red links can't be right clicked anymore.

    $("ul.someLinks1 a").each(function(i, obj) {
    
      $(obj).on("contextmenu",function(){
         return false;
      }); 
      
      $(obj).css("color", "red");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul class="someLinks1">
      <li><a href="www.google.de">google</a></li>
      <li><a href="www.stackoverflow.de">stackoverflow</a></li>
      <li><a href="www.test.de">test</a></li>
    </ul>
    
    <ul class="someLinks2">
      <li><a href="www.foobar.de">foobar</a></li>
      <li><a href="www.foo.de">foo</a></li>
      <li><a href="www.bar.de">bar</a></li>
    </ul>

    0 讨论(0)
  • 2020-12-02 11:42
    $(document).ready(function() {
    
        $(document)[0].oncontextmenu = function() { return false; }
    
        $(document).mousedown(function(e) {
            if( e.button == 2 ) {
                alert('Sorry, this functionality is disabled!');
                return false;
            } else {
                return true;
            }
        });
    });
    

    If you want to disable it only on image click the instead of $(document).mousedown use $("#yourimage").mousedown

    0 讨论(0)
  • 2020-12-02 11:44

    If you're looking into trying to disable the downloading/saving of your images, scripts won't stop that. You would probably have better luck doing this on a server configuration level (like modifying your .htaccess for example on Apache).

    Try asking this on ServerFault.

    0 讨论(0)
  • 2020-12-02 11:48

    The following code will disable mouse right click from full page.

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

    The full tutorial and working demo can be found from here - Disable mouse right click using jQuery

    0 讨论(0)
提交回复
热议问题