单击时将锚标记的链接复制到剪贴板

Aru*_*rma 7 jquery

这是我用来显示一些数据的代码。在这段代码中,我有锚标签,我想在点击它时复制该锚标签的链接。这是我使用的代码如下:

<div class="search_item_list clearfix" id="response">
   <?php foreach($jobs as $job){
   ?>
    <a class="copy_text"  data-toggle="tooltip" title="Copy to Clipboard" 
       href="<?=base_url().'home/company_profile_detail?id='.$job['company_id'];?>"><span class="icon link"><i class="fa fa-link"></i></span>Copy Link</a>
    <?php } ?>
</div>

<script>
   $(".copy_text").click(function(e){
      e.preventDefault();
      var button = $(this);
      var text = button.attr("href");
      text.select();
      $(document).execCommand("copy");
      alert("Copied the text ");
   })
</script>
Run Code Online (Sandbox Code Playgroud)

我得到 jQuery 作为

text.select 不是函数。

Shi*_*hel 10

尝试下面的代码片段

$('.copy_text').click(function (e) {
   e.preventDefault();
   var copyText = $(this).attr('href');

   document.addEventListener('copy', function(e) {
      e.clipboardData.setData('text/plain', copyText);
      e.preventDefault();
   }, true);

   document.execCommand('copy');  
   console.log('copied text : ', copyText);
   alert('copied text: ' + copyText); 
 });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <a class="copy_text"  data-toggle="tooltip" title="Copy to Clipboard" href="home/company_profile_detail">Copy Link</a>
Run Code Online (Sandbox Code Playgroud)