【问题标题】:How to make ClipboardJS copy text along with its hyperlink?如何让 ClipboardJS 复制文本及其超链接?
【发布时间】:2019-08-19 04:32:33
【问题描述】:

如何使ClipboardJS 可以复制文本及其超链接?我已经尝试过使用data-link,但没有成功,

clipboard.on('success', function(e) {
    var aff_link = $(this).attr('data-link'); 
});

这是我已经尝试过的,html是<span id="clipboard" data-clipboard-text="this is a text" data-link="#">copy</span>

【问题讨论】:

    标签: jquery html clipboard.js


    【解决方案1】:

    你可以用hidden input value试试这个。

    var clipboard = new Clipboard('.btn-copy', {
      text: function() {
        return document.querySelector('input[type=hidden]').value;
      }
    });
    clipboard.on('success', function(e) {
      alert("Copied!");
      e.clearSelection();
    });
    $("#input-url").val(location.href);
    .wrapper {
      width: 100%;
      height: 100%;
      text-align: center;
      margin-top: 10px;
    }
    
    .btn-copy {
      background-color: #38AFDD;
      border: transparent;
      border-bottom: 2px solid #0086B7;
      border-radius: 2px;
      padding: 10px;
      min-width: 100px;
      color: #fff;
    }
    
    .btn-copy:hover,
    .btn-copy:focus {
      background-color: #48A1C1;
      border-bottom: 2px solid #38AFDD;
      /*transition cross browser*/
      transition: all .3s ease-in;
      -webkit-transition: all .3s ease-in;
      -moz-transition: all .3s ease-in;
      -o-transition: all .3s ease-in;
    }
    <script src="https://cdn.jsdelivr.net/clipboard.js/1.5.10/clipboard.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="wrapper">
      <h3>Click button below to copy current url to clipboard with hidden input</h3>
      <input type="hidden" id="input-url" value="www.google.com">
      <button class="btn-copy">Copy</button>
    </div>

    【讨论】:

    • 感谢您的回答,但这不是我想要的。我想要的是这样的。当您单击按钮时,文本将被复制,然后当您将其粘贴到 gmail 时,它将显示facebook,并带有超链接https://facebook.com
    【解决方案2】:
    Easy Code ;)
    
    <!-- The text field -->
        <input type="text" value="Hello World" id="myInput">
    
        <!-- The button used to copy the text -->
        <button onclick="myFunction()">Copy text</button>
    
    
        function myFunction() {
          /* Get the text field */
          var copyText = document.getElementById("myInput");
    
          /* Select the text field */
          copyText.select();
    
          /* Copy the text inside the text field */
          document.execCommand("copy");
    
          /* Alert the copied text */
          alert("Copied the text: " + copyText.value);
        }
    

    【讨论】:

    • 感谢您的回答!但我认为您的代码只复制文本,而不是超链接,我需要一种复制富文本格式的方法,因为我希望将超链接复制到剪贴板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2016-02-25
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多