javascript复制网页内容 execCommand(´ Copy´ )

    xiaoxiao2021-12-14  18

    javascript复制网页内容 execCommand(´ Copy´ )

    大概就这几种主要方法,前三种要么过于繁琐不太好使要么兼容性不好,今天提供的方法就是最后一种,推荐使用

    **window.clipboardData (IE 才有)

    **ZeroClipboard (借助Flash)

    **window.prompt

    **document.execCommand


    在js里添加这个方法,传入的参数为你将要复制的文字,方法的返回值为是否复制成功

    function copyTextToClipboard(text) { var textArea = document.createElement("textarea") textArea.style.position = 'fixed' textArea.style.top = 0 textArea.style.left = 0 textArea.style.width = '2em' textArea.style.height = '2em' textArea.style.padding = 0 textArea.style.border = 'none' textArea.style.outline = 'none' textArea.style.boxShadow = 'none' textArea.style.background = 'transparent' textArea.value = text document.body.appendChild(textArea) textArea.select() var msg = ''; try { msg = document.execCommand('copy') ? '复制成功!' : '复制失败!' } catch (err) { msg = '不能使用这种方法复制内容' } document.body.removeChild(textArea) return msg; }

    转自: http://www.jianshu.com/p/37322bb86a48


    网上找了几个小时,才写好这个功能。第一次写博客,多多关照,也为方便广大跟我一起学习的码农们,共同进步!

    转载请注明原文地址: https://ju.6miu.com/read-971939.html

    最新回复(0)