如何使用jquery将文本复制到剪贴板? [重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

大家好我试图使用jquery复制文本框值,但我不想使用添加插件这里是我的代码

 <div class="form-group col-lg-4 col-md-4 col-sm-12 col-xs-12"> 
  <input type="text" id="visor_node_token" name="view" value="Hello World">
 </div>
 <input type="button" name="view" value="Copy" class="btn btn-primary" onclick="copy_node_token()" />
function copy_node_token()
{
  var node = $("#visor_node_token").val();
  $(node).select();
  document.execCommand("Copy");
}`

我无法复制文本值。请帮我解决这个问题

javascript jquery
1个回答
1
投票

 <!DOCTYPE html>

  <style>
    #t {
      width: 1px
      height: 1px
      border: none
    }
    #t:focus {
      outline: none
    }
  </style>

  <script>
    function copy(text) {
      var t = document.getElementById('t')
      t.innerHTML = text
      t.select()
      try {
        var successful = document.execCommand('copy')
        var msg = successful ? 'successfully' : 'unsuccessfully'
        console.log('text coppied ' + msg)
      } catch (err) {
        console.log('Unable to copy text')
      }
      t.innerHTML = ''
    }
  </script>

  <textarea id=t></textarea>

  <button onclick="copy('hello world')">
    Click me
  </button>
© www.soinside.com 2019 - 2024. All rights reserved.