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

时间:2017-12-19 12:00:24

标签: javascript jquery

大家好我想尝试使用jquery复制文本框值,但我不想在插件上使用add 这是我的代码

 <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 个答案:

答案 0 :(得分: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>