function copyStr(){
document.addEventListener('click', function copy(e) {
dom = document.createElement('textarea');
dom.style.fontSize = '12pt';
dom.style.border = '0';
dom.style.padding = '0';
dom.style.margin = '0';
dom.style.position = 'absolute';
dom.style['left'] = '-9999px';
var yPosition = window.pageYOffset || document.documentElement.scrollTop;
dom.style.top = yPosition + 'px';
dom.setAttribute('readonly', '');
dom.value = "复制到粘贴板的内容";
document.body.appendChild(dom);
dom.select();
dom.setSelectionRange(0, dom.value.length);
document.execCommand('copy');
document.removeEventListener('click', copy);
})
}
这个方法必须有点击事件调用才可以,因为复制这个动作需要用户主动触发