jquery implemented selective disable right mouse button

Source: Internet
Author: User
Tags bind tagname

The selective disabling of the right mouse button is implemented in jquery:

The use of more violent means to disable the right mouse button is not humane, so it is best to selectively disable the right mouse button.

The code example is as follows:

<! DOCTYPE html> <meta charset= "Utf-8" >&NBSP;
<meta name= " Author "content=" http://www.45it.com//> 
<title> computer hardware and software application network </title> 
<style type = "Text/css"
html,body{height:100%}
div{
  width:150px;
  height:50px;
  Background: #CCC;
}
</style>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script
<script type= "Text/javascript" > 
$ (document). Ready (function () {
  function Jquery_ Istagname (Ev,arr) {
    ev=$.event.fix (EV);
    var target=ev.target| | Ev.srcelement;
    if (Arr&&$.inarray (target.tagName.toString (). toUpperCase (), arr) ==-1) {
       return false;
   }
    return true;
 }
 
  $ (document). Bind ("ContextMenu", FunctioN (EV) {
    if (!jquery_istagname (ev,[' INPUT ', ' TEXTAREA '))} {
      Ev.preventdefault ();
      return false;
   }
    return true;
 })
})
</script> 
<body>
<div id= "Thediv" ></div> &NBSP
<textarea></textarea>
</body> 

The code above implements our requirements, and the following is an introduction to the implementation process of the code.

Code comments:

1.$ (document). Ready (function () {}) to execute code in the function when the text structure is completely loaded.
2.function Jquery_istagname (Ev,arr) {}, this function can determine whether the element can be used in the right-click menu, the first argument is the event object, the second parameter is an array, and the array element is the name of the label that can use the right-click menu.
3.ev=$.event.fix (EV), which implements the compatibility of event objects in various browsers, the fix () function is used inside jquery, and of course it can be used as well.
4.var target=ev.target| | Ev.srcelement, gets the event source object.
5.if (Arr&&$.inarray (Target.tagName.toString (). toUpperCase (), arr) ==-1) {return false;}, to determine whether the specified label element is in the array, Returns FALSE if the specified label is not in the array.
6.return true to return true.
8.$ (document). Bind ("ContextMenu", function (EV) {}) to register the ContextMenu event handler function for document documents.
9.if (!jquery_istagname (ev,[' INPUT ', ' TEXTAREA '])) {
Ev.preventdefault ();
return false;
}

If you specify that the label is not in the list where you can use the right-click menu, then it is important to use Ev.preventdefault () to block event bubbling, otherwise if there are elements nested, although the child element disables the right-click menu, but the right key child elements, or the right button pop-up menu, Because the event is passed to the parent element, return False can also disable the right-click menu.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.