步骤
1. 先拿到要复制的DOM中的值
//document.getElementById("wordText").innerText 被复制的DOM中的文本值
2. 创建一个可选中的文本框 document.createElement("textarea")
3. 调用 document.execCommand("copy") 进行复制 直接粘贴就可以看到复制的值了
let textArea = document.createElement("textarea");
textArea.value = document.getElementById("wordText").innerText;
// 固定文本框的位置
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
// 选中文本框
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
js实现点击复制功能
最新推荐文章于 2024-05-07 08:39:05 发布