Home > Article > Web Front-end > JS operation clipboard code sharing
Javascript can easily manipulate client clipboard content, but it is only applicable to IE5 and above browsers. JavaScript can handle clipboard contents using the window.clipboardData object. Method setData(param1, param2) to save to the clipboard. This article mainly introduces how to use JS to operate the clipboard function, and shares the example code. Friends who need it can learn from it. I hope it can help everyone.
param1: Data type text or URL, etc.
param2: Data content.
The method of reading data from the clipboard getdata(param1)
The method of clearing data clearData(param1)
<HTML> <HEAD> <TITLE>测试操作剪贴板</TITLE> </HEAD> <script> function copyToClipboard() { var d=document.all("source").value; window.clipboardData.setData('text',d); } </script> <BODY> <button onclick="copyToClipboard();">拷贝</button> <input type="text" size=20 id="source" value="测试数据"> <br> <button onclick="alert(window.clipboardData.getData('text'));">显示</button> <button onclick="window.clipboardData.clearData('text');">清空</button> </BODY> </HTML> 下面是另一个例子实现页面中选中字符,并拖拉到文本区功能。注意其中的window.event.dataTransfer对象也可处理剪贴板内容,不过只能用在 drag-and-drop 操作中。 <HTML> <HEAD> <TITLE>测试操作剪贴板2</TITLE> </HEAD> <script> function transferDrop() { window.event.srcElement.innerText = window.event.dataTransfer.getData("text"); window.event.returnValue = false; } function transferDrag() { window.event.dataTransfer.dropEffect = 'move'; window.event.returnValue = false; } </script> <BODY> <p id="mySource" ondragstart="window.event.dataTransfer.effectAllowed = 'move';">选择我们并把我们拖到下面的textarea</p> <textarea id="myTarget" ondrop="transferDrop();" ondragover="window.event.returnValue = false;" ondragenter="transferDrag();"> </textarea> </BODY> </HTML>
Related recommendations:
Summary of JavaScript methods for copying to the clipboard
HTML5 actual combat and analysis of the clipboard Event
javascript - html5 mobile development, how to copy to the clipboard?
The above is the detailed content of JS operation clipboard code sharing. For more information, please follow other related articles on the PHP Chinese website!