這兩天在做Web前端時(shí),遇到需求通過(guò)js實(shí)現(xiàn)文本復(fù)制的功能。經(jīng)過(guò)一番測(cè)試,終于實(shí)現(xiàn)了出來(lái),有需要的小伙伴可以參考下。
分享一段利用 JavaScript 實(shí)現(xiàn)復(fù)制功能的代碼,兼容多瀏覽器,兼容IE和火狐瀏覽器。
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>JavaScript 復(fù)制功能代碼,兼容多瀏覽器</title>
- </head>
- <script language="javascript">
- copyValue=function(strValue){
- if(isIE()){
- clipboardData.setData("Text",strValue);
- alert("您已成功復(fù)制了此地址");
- }else{
- copy(strValue);
- alert("內(nèi)容已被復(fù)制!");
- }
- }
- function isIE(number){
- if(typeof(number)!=number){
- return!!document.all;
- }
- }
- function copy(text2copy){
- var flashcopier = 'flashcopier';
- if(!document.getElementById(flashcopier)){
- var divholder = document.createElement('div');
- divholder.id = flashcopier;
- document.body.appendChild(divholder);
- }
- document.getElementById(flashcopier).innerHTML = '';
- var divinfo = '<embed src="http://files.vevb.com/demoimg/200910/_clipboard.swf" FlashVars="clipboard='+text2copy+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';//這里是關(guān)鍵
- document.getElementById(flashcopier).innerHTML = divinfo;
- }
- </script>
- <div class="phoinfo">
- 貼圖地址:<input name="txtPhotoPath" value="www.daimajiayuan.com" id="txtPhotoPath" type="text" size="65" />
- <input type="button" name="btnCopy" id="btnCopy" onClick="copyValue('www.daimajiayuan.com');" value="復(fù)制" />
- </div>
- </body>
- </html>
以上所述就是本文給大家的分享的全部?jī)?nèi)容了,希望大家能夠喜歡。
新聞熱點(diǎn)
疑難解答
圖片精選