Native copy the text to achieve js

html:
<div id="copyText" style="padding: 20px 40px 0 40px;">账号:123456,密码:123457</div>
<input id="copyInput" style="opacity:0;"></input>
                      
js:
var text = document.getElementById("copyText").innerText;
var input = document.getElementById("copyInput");
document.getElementById("copyInput").value = text; // 修改文本框的内容
input.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令

First, if the need to copy the text in the input or textarea
1. acquired dom, using .Select () to select.
2. Use document.execCommand ( "copy"); to copy.

Second, if you need to copy the text in the textarea not input or
1. first get to the text you want to copy.
2. assigned to input text or textarea.
3. input or textarea patterns may set opacity: 0; thus transparent.
4. then to the top "a" in steps 1 and 2.

Guess you like

Origin www.cnblogs.com/hukuangjie/p/12147438.html