代码实现 (吐槽:百度上各种方法,就是不好使。或者就是引用第三方插件,去stackoverflow,一下就解决,所以啊,多用谷歌)
已测试谷歌和IE(8)
function copyStringToClipboard (str) {
// Create new element
var el = document.createElement('textarea');
// Set value (string to be copied)
el.value = str;
// Set non-editable to avoid focus and move outside of view
el.setAttribute('readonly', '');
//el.style = {position: 'absolute', left: '-9999px'}; //IE不支持这种写法
el.style.position = "absolute";
el.style.left="-9999px";
document.body.appendChild(el);
// Select text inside element
el.select();
// Copy text to clipboard
document.execCommand('copy');
// Remove temporary element
document.body.removeChild(el);
}