js复制功能(pc复制,移动端复制到手机剪切板)

一个函数,直接调就好了,已测pc和app都适用

 1     // 一键复制
 2     copyBtn(data) {
 3       const input = document.createElement("input");
 4       input.setAttribute("readonly", "readonly");
 5       input.setAttribute("value", data);
 6       document.body.appendChild(input);
 7       input.focus();
 8       input.setSelectionRange(0, 9999);
 9 
10       if (document.execCommand("copy")) {   // true
11         document.execCommand("copy");
12         console.log("复制成功");
13       }
14       document.body.removeChild(input);
15     },
原文地址:https://www.cnblogs.com/shun1015/p/14152370.html