js实现点击复制文本内容

<script type="text/javascript">
                function copyUrl(b){
                    console.log(b);
                    var Url2=document.getElementById(b).innerText;
                    console.log(Url2);
                    var oInput = document.createElement('input');     //创建一个隐藏input
                    console.log(oInput);
                    oInput.value = Url2;    //赋值
                    console.log(oInput.value);
                    document.body.appendChild(oInput);
                    oInput.select(); // 选择对象
                    document.execCommand("Copy"); // 执行浏览器复制命令
                    oInput.className = 'oInput';
                    oInput.style.display='none';
                    alert('复制成功');
                }
            </script>
            <div cols="20" id="biao1">1111<img src="/static/paste/copycode.gif" onclick="copyUrl('biao1')" alt="点击复制"/></div>
            <div cols="20" id="biao2">2222<img src="/static/paste/copycode.gif" onclick="copyUrl('biao2')" alt="点击复制"/></div>
            <div cols="20" id="biao3">3333<img src="/static/paste/copycode.gif" onclick="copyUrl('biao3')" alt="点击复制"/></div>
            <div cols="20" id="biao4">4444<img src="/static/paste/copycode.gif" onclick="copyUrl('biao4')" alt="点击复制"/></div>
            <div cols="20" id="biao5">5555<img src="/static/paste/copycode.gif" onclick="copyUrl('biao5')" alt="点击复制"/></div>
            <div cols="20" id="biao6">6666<img src="/static/paste/copycode.gif" onclick="copyUrl('biao6')" alt="点击复制"/></div>
            <div cols="20" id="biao7">7777<img src="/static/paste/copycode.gif" onclick="copyUrl('biao7')" alt="点击复制"/></div>
            <div cols="20" id="biao8">8888<img src="/static/paste/copycode.gif" onclick="copyUrl('biao8')" alt="点击复制"/></div>

 

//以下是用js实现下载页面数据

function getTdValue() {
            var tableId = document.getElementById("example0");
            var a = new Array();

            for (var i = 1; i < tableId.rows.length; i++) {
                a.push(tableId.rows[i].cells[0].innerHTML + '\r\n');
            }
            var mnb = a.join('');

            mnb = mnb + '\r\n' + '\r\n' + 'More IP ' + document.domain;
            var sss = getNowFormatDate();
            doSave(mnb, "text/latex", sss);
        }

        function doSave(value, type, name) {
            var blob;
            if (typeof window.Blob == "function") {
                blob = new Blob([value], {type: type});
            } else {
                var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder;
                var bb = new BlobBuilder();
                bb.append(value);
                blob = bb.getBlob(type);
            }
            var URL = window.URL || window.webkitURL;
            var bloburl = URL.createObjectURL(blob);
            var anchor = document.createElement("a");
            if ('download' in anchor) {
                anchor.style.visibility = "hidden";
                anchor.href = bloburl;
                anchor.download = name;
                document.body.appendChild(anchor);
                var evt = document.createEvent("MouseEvents");
                evt.initEvent("click", true, true);
                anchor.dispatchEvent(evt);
                document.body.removeChild(anchor);
            } else if (navigator.msSaveBlob) {
                navigator.msSaveBlob(blob, name);
            } else {
                location.href = bloburl;
            }
        }
function getNowFormatDate() {
            var domain = document.domain;
            var date = new Date();
            var year = date.getFullYear();
            var mon = ("0" + (date.getMonth() + 1)).slice(-2);
            var da = ("0" + (date.getDate())).slice(-2);
            var h = ("0" + (date.getHours())).slice(-2);
            var m = ("0" + (date.getMinutes())).slice(-2);
            var name = domain + mon + da + h + m + '.txt';
            return name;
        }

 

没有更多推荐了,返回首页