【问题标题】:how to copy text to clipboard using jquery? [duplicate]如何使用 jquery 将文本复制到剪贴板? [复制]
【发布时间】:2018-06-01 20:42:52
【问题描述】:

大家好,我正在尝试使用 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");
}`

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

【问题讨论】:

标签: javascript jquery


【解决方案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>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 1970-01-01
  • 2010-12-01
  • 2011-10-06
  • 1970-01-01
相关资源
最近更新 更多