Home > Article > Web Front-end > Click copy effect made with JS
This time I will bring you the click-to-copy effect made with JS, the click-to-copy effect made with JSWhat are the precautions?The following is a practical case, let's take a look.
Principle:
Simulate calling the browser Copy command through the textarea data in the all-select form to achieve copying
Example
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>复制</title></head><body> <script> var textCopy=function (data) { var f=document.createElement("form"); f.id="copy-"+Date.parse(new Date()); f.onsubmit=function(){return false}; f.style="opacity: 0;height: 1px;width: 1px;overflow: hidden;position:fixed;top: -1;left: -1;z-index: -1;" f.innerHTML=`<button onclick='story.select();document.execCommand("Copy");'></button> <textarea name="story">${data}</textarea>`; document.body.appendChild(f); document.querySelector(`#${f.id}>button`).click(); document.body.removeChild(document.getElementById(f.id)); } </script> <input id="test" value="" /> <button onclick="textCopy(document.getElementById('test').value)" >复制</button> </body></html>
I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
Nodejs How to use http module to write upload image interface test client
h5 Make performance change line chart
How nodejs implements single sign-on Demo
The above is the detailed content of Click copy effect made with JS. For more information, please follow other related articles on the PHP Chinese website!