一:如果你想复制的是 input 或 textarea 里的内容
<textarea id="input_copy">页面上展示的内容</textarea>
<button>点我复制内容</button>
<script>
copyHref() {
let copyInp = document.getElementById('input_copy');
copyInp.select();
document.execCommand("copy");
}
</script>
二:如果你想复制的不是 input 或 textarea 里的内容
<p id="pText">页面上展示的内容</p>
<button>点我复制内容</button>
<script>
copyHref() {
// 创建一个DOM元素 textarea
let copyInp = document.createElement('textarea');
// 设置高度为0 透明度为0
copyInp.style.height = 0
copyInp.style.opacity = 0
// 添加到页面上
document.body.appendChild(copyInp)
// 将需要复制的内容赋值给textarea
copyInp.innerHTML = pText.innerHTML
copyInp.select();
document.execCommand("copy");
}
</script>