JS如何实现剪贴板里自动加上版权出处
JavaScript 如何实现复制文本添加出处,下面来看看剪贴板里自动加上版权出处的技术js如何实现。
可以用如下代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script> function addLink() { var body_element = document.body; var selection; var blogName = '网站名称'; selection = window.getSelection() ? window.getSelection() : document.selection.createRange().text; if (window.clipboardData) { var pagelink ="<br/>---------------------<br/>"+"<br/>原文地址:"+document.location.href+""; var copyText = selection + pagelink; window.clipboardData.setData ("Text", copyText); return false; } else { var pagelink = "<br/>---------------------<br/>"+"<br/>原文地址:"+document.location.href+""; var copyText = selection + pagelink; var newDiv = document.createElement('div'); newDiv.style.position ='absolute'; newDiv.style.left ='-123456789px'; body_element.appendChild(newDiv); newDiv.innerHTML = copyText; selection.selectAllChildren(newDiv); window.setTimeout(function() { body_element.removeChild(newDiv); },0); } } document.oncopy = addLink; </script> |