转自:https://www.cnblogs.com/zdz8207/p/js-oncopy.html
1 var ua = navigator.userAgent.toLowerCase(); 2 if( window.ActiveXObject ){ 3 document.body.oncopy = function(){ 4 event.returnValue = false; 5 var t=document.selection.createRange().text; 6 var s=" 原文链接:"+location.href; 7 clipboardData.setData('Text',t+' '+s); 8 }; 9 }else{ 10 function addLink(){ 11 var body_element = document.getElementsByTagName('body')[0]; 12 var selection; 13 selection = window.getSelection(); 14 var pagelink = " 原文链接:"+location.href; 15 var copytext = selection + pagelink; 16 var newdiv = document.createElement('div'); 17 newdiv.style.position='absolute'; 18 newdiv.style.left='-99999px'; 19 body_element.appendChild(newdiv); 20 newdiv.innerHTML = copytext; 21 selection.selectAllChildren(newdiv); 22 window.setTimeout(function(){body_element.removeChild(newdiv);},0); 23 } 24 document.oncopy = addLink; 25 }