Javascript 复制功能对隐藏的输入类型不起作用

奥塔维奥·巴雷托

我试图保留input type=hidden但它不复制值,我需要隐藏输入字段并保持复制功能完好无损,有人有解决方案吗?

<button class="btn btn-primary" onclick="copy_link()">copy;<i class="fa fa-check"></i></button>
<input  id="my_link" type="text" class="form-control" value="test123 " />
<script>
  function copy_link() {
  /* Get the text field */
  var copyText = document.getElementById("my_link");

  /* Select the text field */
  copyText.select();

  /* Copy the text inside the text field */
  document.execCommand("copy");
$('#copy_alert').show();
 
}

$(document).ready(function() {
$('#copy_alert').hide();
});
  </script>

奥塔维奥·巴雷托

我自己解决了这里是解决方案:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<script>
  function setClipboard(value) {
    var tempInput = document.createElement("input");
    tempInput.style = "position: absolute; left: -1000px; top: -1000px";
    tempInput.value = value;
    document.body.appendChild(tempInput);
    tempInput.select();
    document.execCommand("copy");
    document.body.removeChild(tempInput);
    $('#copy_alert').show();
}
$(document).ready(function() {
$('#copy_alert').hide();
});

</script>
<button onclick="setClipboard('copytest')" class="btn btn-light">Copy</button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章