Using brute force means to disable the right mouse button is not user-friendly, so it is best to disable the right mouse button selectively.
The code example is as follows:
<! DOCTYPE html>
The above Code meets our requirements. Next we will introduce the implementation process of the Code.
Code comment:
1. $ (document). ready (function () {}). When the text structure is fully loaded, run the code in the function.
2. function jQuery_isTagName (ev, arr) {}. This function can be used to determine whether an element is in the right-click menu column. The first parameter is the event object, and the second parameter is an array, the array element is the label name that can be right-clicked.
3. ev = $. event. fix (ev) to implement the compatibility of event objects in various browsers. The fix () function is used internally by jquery. Of course, this can also be used.
4. var targetincluev.tar get | ev. srcElement to obtain the event source object.
5. if (arr & $. inArray (target. tagName. toString (). toUpperCase (), arr) =-1) {return false;}: determines whether the specified tag element is in the array. If no tag is specified in the array, false is returned.
6. return true, return true.
8. $ (document). bind ("contextmenu", function (ev) {}), register the contextmenu event handler for the document.
9. if (! JQuery_isTagName (ev, ['input', 'textarea ']) {
Ev. preventDefault ();
Return false;
}
If the specified tag is not in the right-click menu, ev is used. preventDefault () is important to prevent event bubbling. Otherwise, if element Nesting is enabled, the context menu will pop up when you right-click the child element, because the event is passed to the parent element, return false can also disable the right-click menu.