提问者:小点点

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


大家好,我正在尝试使用jquery复制textbox值,但我不想使用add on插件,这里是我的代码

 <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");
}`

我无法复制文本值。请帮助我


共1个答案

匿名用户

null

 <!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>