首頁  >  文章  >  web前端  >  用JS做的點擊複製效果

用JS做的點擊複製效果

php中世界最好的语言
php中世界最好的语言原創
2018-03-12 11:37:311784瀏覽

這次帶給大家用JS做的點擊複製效果,用JS做的點擊複製效果注意事項有哪些,下面就是實戰案例,一起來看一下。

原則:
模擬透過全選表單內的textarea資料呼叫瀏覽器Copy指令實現複製

範例

<!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>

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

相關閱讀:

nodejs如何使用http模組寫上傳圖片介面測試客戶端

h5製作效能變化折線圖

nodejs怎麼透過jsonp來實作單一登入Demo

#

以上是用JS做的點擊複製效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn