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

  1. >  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  6.     <title>Documenttitle>  
  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.                   /*  
  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 Testtitle>  
  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>  




你可能感兴趣的:(javascript)