📜  Javascript 复制文本 onclick - 任何代码示例

📅  最后修改于: 2022-03-11 14:57:22.499000             🧑  作者: Mango

代码示例1
const span = document.querySelector("span");

span.onclick = function() {
  document.execCommand("copy");
}

span.addEventListener("copy", function(event) {
  event.preventDefault();
  if (event.clipboardData) {
    event.clipboardData.setData("text/plain", span.textContent);
    console.log(event.clipboardData.getData("text"))
  }
});