隐藏的元素不会复制到剪贴板 [英] Hidden element won't copy to clipboard

查看:97
本文介绍了隐藏的元素不会复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add button to copy simple text string but without success.

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>

它不会在剪贴板中放置任何东西.

It does not put anything in Clipboard.

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

I do not need input field. I can add text in JS itself.

推荐答案

使用屏幕外类和aria-hidden属性(后者用于可访问性)代替hidden属性:

Instead of the hidden attribute, use an offscreen class and the aria-hidden attribute (the latter for accessibility):

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

<input ... class="offscreen" aria-hidden="true">

这篇关于隐藏的元素不会复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆