拷贝页面文本到粘贴板(兼容IE8)
function copy() {
//根据实际情况更改,需要复制内容的载体
var cpTxt = $(this).parent().prev("td").text();
if (!cpTxt) {
alert("内容为空");
return null;
}
//生成一个textarea对象
var textArea = document.createElement('input');
//设置属性
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 = cpTxt;
//添加到页面body
document.body.appendChild(textArea);
textArea.select();
//执行
var bool = document.execCommand('copy');
var msg = bool ? '成功' : '失败';
alert('复制' + msg);
//移除对象
document.body.removeChild(textArea);
}