您的位置:首页 > Web前端 > JavaScript

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

2016-12-04 02:12 316 查看

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

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

**window.clipboardData (IE 才有)

**ZeroClipboard (借助Flash)

**window.prompt

**document.execCommand

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

function copyTextToClipboard(text) {
var textArea = document.createElement("textarea")

textAre
4000
a.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

网上找了几个小时,才写好这个功能。第一次写博客,多多关照,也为方便广大跟我一起学习的码农们,共同进步!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息