js实现点击复制按钮复制文本

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  6.     <title>Document</title>  
  7. </head>  
  8. <body>  
  9.     <script data-cfhash='f9e31' type="text/javascript">  
  10.                   function jsCopy(){  
  11.                     var e=document.getElementById("copy-num");//对象是copy-num1  
  12.                     e.select(); //选择对象  
  13.                     document.execCommand("Copy"); //执行浏览器复制命令  
  14.                     alert("复制成功");  
  15.                 }  
  16.                   /* <![CDATA[ */ 
  17.                   ! 
  18.                   function() { 
  19.                     try { 
  20.                       var t = "currentScript" in document ? document.currentScript: function() { 
  21.                         for (var t = document.getElementsByTagName("script"), e = t.length; e--;) if (t[e].getAttribute("data-cfhash")) return t[e] 
  22.                       } (); 
  23.                       if (t && t.previousSibling) { 
  24.                         var e, r, n, i, c = t.previousSibling, 
  25.                         a = c.getAttribute("data-cfemail"); 
  26.                         if (a) { 
  27.                           for (e = "", r = parseInt(a.substr(0, 2), 16), n = 2; a.length - n; n += 2) i = parseInt(a.substr(n, 2), 16) ^ r, 
  28.                           e += String.fromCharCode(i); 
  29.                           e = document.createTextNode(e), 
  30.                           c.parentNode.replaceChild(e, c) 
  31.                         } 
  32.                         t.parentNode.removeChild(t); 
  33.                       } 
  34.                     } catch(u) {} 
  35.                   } () 
  36.                   /* ]]> */  
  37.                     
  38.                 </script>  
  39.                  <div class="nav navbar-top-links navbar-right">  
  40.                   <li>推广链接:</li>  
  41.                    
  42.                     <li><input id="copy-num" class="form-control" type="text" value="" style="max-width:100px;display:inline-block;color: #999; margin-top:12px"></li>  
  43.                     <li><button onClick="jsCopy()" type="button" class="btn btn-sm">复制</button></li>  
  44.                    
  45.                 </div>  
  46. </body>  
  47. </html>  


eg2.

  1. <html>  
  2. <head>  
  3.     <meta charset="UTF-8">  
  4.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  5.     <title>Zero Clipboard Test</title>  
  6. </script>  
  7. </head>  
  8. <body>  
  9.    <script type="text/javascript">   
  10.     function jsCopy(){   
  11.         var e=document.getElementById("contents");//对象是contents   
  12.         e.select(); //选择对象   
  13.         tag=document.execCommand("Copy"); //执行浏览器复制命令  
  14.         if(tag){  
  15.           alert('复制内容成功');  
  16.         }  
  17.     }   
  18.   
  19. </script>   
  20. <textarea id="contents" cols="40" rows="5"></textarea>  
  21. <br />  
  22. <input type="button" onClick="jsCopy();" value="复制" />  
  23. </body>  
  24. </html>  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值