首页 > 代码库 > js点击后将文字复制到剪贴板,将图片复制到剪贴板

js点击后将文字复制到剪贴板,将图片复制到剪贴板

 复制文字:

[html] view plaincopy
  1. <table width="99%" border="0" cellpadding="0" cellspacing="0" class="firtable">  
  2.     <tr>  
  3.     <th width="100%" style="color: white;"><s:text name="询单明细"></s:text></th>  
  4.    </tr>  
  5.   <tr>  
  6.     <td align="center">   
  7.       <textarea name="inquiryContact1" id="inquiryContact1" rows="15" cols="60" readonly="readonly"></textarea>  
  8.         <div id="inquiryInfoDIV" style="display:none">  
  9.            <s:property value=http://www.mamicode.com/"inquiryContact" escape="false"/>  
  10.        </div>  
  11.        <script>          dojo.byId("inquiryContact1").innerText=dojo.byId("inquiryInfoDIV").innerText;  
  12.       </script>  
  13.     </td>  </tr>  
  14.   <tr>  
  15.     <td align="center">  
  16.       <input type="button" id="button" name="button" value=http://www.mamicode.com/"复制" onclick="copyContact()"/>  
  17.     </td</tr</table>  

 

[javascript] view plaincopy
  1. <script type="text/javascript">  
  2.     var i = 0 ;   
  3.     function copyContact(){  
  4.         var contat = document.getElementById("inquiryContact1").value;  
  5.         window.clipboardData.setData(‘text‘, contat);  
  6.         if(window.clipboardData.getData(‘text‘)==‘‘){  
  7.             if(i==1){  
  8.                 alert("复制失败,请手动Ctrl+C快捷键复制!");  
  9.             }else{  
  10.                 alert("复制失败,请重新复制!");  
  11.                 i = 1;  
  12.             }  
  13.         }else{  
  14.              alert("内容已经复制到剪贴板!");  
  15.         }  
  16.     }  
  17. </script>  

 

复制图片:

[html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. <SCRIPT LANGUAGE="JScript">  
  7. var oPopup = window.createPopup();  
  8. function ButtonClick(div)  
  9. {  
  10. //var div = document.getElementById(‘divId‘);  
  11. div.contentEditable = ‘true‘;  
  12. var controlRange;  
  13. if (document.body.createControlRange) {  
  14. controlRange = document.body.createControlRange();  
  15. controlRange.addElement(div);  
  16. controlRange.execCommand(‘Copy‘);  
  17. }  
  18. div.contentEditable = ‘false‘;  
  19. }  
  20. </SCRIPT>  
  21. </head>  
  22. <body>  
  23.     <div id="divId1">  
  24.         <img src=http://www.mamicode.com/"F:/2012070518474964.jpg" onclick="ButtonClick(this)">  
  25.     </div>  
  26. </BODY>  
  27. </body>  
  28. </html