How to disable pasting in JavaScript: 1. Bind the onpaste event to the element and set the event processing function; 2. In the event processing function, set the "return false;" statement to indicate that when the paste event is triggered, return false.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, you can use the onpaste event to disable pasting. The onpaste event is triggered when the user pastes text into an element.
Note: Although the HTML elements used all support the onpaste event, not all elements, such as the
element, are actually supported unless contenteditable is set to "true" (see more below) Example).
Tip: The onpaste event is usually used for elements of type="text".
Tip: There are three ways to paste content in an element:
Press CTRL V
From the browser's edit menu Select "Paste"
Right-click the mouse button and select the "Paste" command in the context menu.
Syntax
In HTML:
<element onpaste="myScript">
In JavaScript:
object.onpaste = function(){ //操作 myScript; } JavaScript中,使用addEventListener()方法: object.addEventListener('paste',myScript);2 //Internet Explorer 8 及更早 IE 版本不支持 addEventListener() 方法。
Implementation principle:
Execute copy and paste events and return false in the event.
JavaScript code:
var bodyMain = document.getElementById('bodyMain' ); //禁止复制 bodyMain.oncopy = function(){ return false; } //禁止粘贴 bodyMain.onpaste = function(){ return false; }
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to disable pasting in javascript. For more information, please follow other related articles on the PHP Chinese website!