隐藏元素不会复制到剪贴板

2023-11-21

我正在尝试添加按钮来复制简单的文本字符串,但没有成功。

function kopiraj() {
  var copyText = document.getElementById("toCopy");
  copyText.select();
  document.execCommand("Copy");
  document.getElementById("telefon").innerHTML = 'Copied';
}
<button type="button" onclick="kopiraj()">Copy</button>
<input type="hidden" id="toCopy" value="123456789">
<p id="telefon"></p>

它不会将任何内容放入剪贴板。

我不需要输入字段。我可以在 JS 本身中添加文本。


而不是hidden属性,使用屏幕外类和aria-hidden属性(后者用于可访问性):

.offscreen {
    position: absolute;
    left: -999em;
}

<input ... class="offscreen" aria-hidden="true">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

隐藏元素不会复制到剪贴板 的相关文章