代码描述:javascript 浏览器 复制黏贴效果。javascript兼容所有浏览器的点击复制黏贴效果
点击复制指定内容在在js中有相应的代码,但是浏览器兼容性很差,下面分享一个结合flash实现的这样的效果,能够完美兼容所有主流浏览器,希望能够给需要的朋友带来一定的帮助 代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.51qianduan.com/" /> <title>js实现的剪切板功能-51前端</title> <script type="text/javascript"> function copy_code(copyText) { if(window.clipboardData) { window.clipboardData.setData("Text", copyText) } else { 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="mytest/JS/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(copyText)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>'; document.getElementById(flashcopier).innerHTML = divinfo; } alert('复制成功!'); } window.onload=function() { var thediv=document.getElementById("thediv"); var bt=document.getElementById("bt"); bt.onclick=function() { copy_code(thediv.innerHTML); } } </script> <body> <div id="thediv">51前端欢迎您</div> <input id="bt" type="button" value="点击拷贝div中的内容" /> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能