<div>这里是待复制的文字或图片</div>
var range = document.createRange(); var referenceNode = document.getElementsByTagName("div")[0]; range.selectNode(referenceNode); window.getSelection().addRange(range); try { var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'failed'; console.log('Copy ' + msg); } catch(err) { console.log('Oops, unable to copy');
clipboardData.setData('Text', "Some data"); }
// Remove the selections - NOTE: Should use
// removeRange(range) when it is supported
window.getSelection().removeAllRanges();
如此一来就能省去了制作flash的麻烦。