Home  >  Article  >  Web Front-end  >  Click copy effect made with JS

Click copy effect made with JS

php中世界最好的语言
php中世界最好的语言Original
2018-03-12 11:37:311803browse

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=&#39;story.select();document.execCommand("Copy");&#39;></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(&#39;test&#39;).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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn