页面实现复制功能

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <button onclick="copyToCliper('复制的实际内容')">按钮</button></div>
        <script type="text/javascript"> 
            function copyToCliper(msg){       
                var Sys = {}; 
                var ua = navigator.userAgent.toLowerCase(); 
                var s; 
                (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : 
                (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : 
                (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : 
                (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : 
                (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0; 
                if( Sys.ie ){
                    var _text = document.createElement("textarea");
                    _text.style.width = "1px";
                    _text.style.height = "1px";
                    _text.style.background = "transparent";
                    _text.style.border = "none";
                    _text.style.resize = "none";
                    _text.style.filter = "alpha(opacity=0)";
                    _text.textContent = msg;
                    document.body.appendChild(_text);
                    _text.select(); //选择对象 
                    document.execCommand("Copy"); //执行浏览器复制命令
                    alert("已复制。");
                }
                else{
                    window.prompt("您的浏览器不支持直接复制\n请使用Ctrl+C来复制文本框内容,确认完成后点击关闭。",msg);
                }
            } 
        </script>
    </body>
</html>

  

posted @ 2014-12-06 09:45  junxizai  阅读(268)  评论(0编辑  收藏  举报