<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JS 复制时自动加上网址</title> </head> <body> <h2>阅谁问君诵,水落清香浮。</h2> <script type="text/javascript"> document.body.oncopy = function() { setTimeout(function() { var text = clipboardData.getData("text"); if(text) { text = text + "\r\n文章来自: 网站名称(http://www.onestopweb.cn) 详文参考:" + location.href; clipboardData.setData("text", text); } }, 100) } </script> </body> </html>
PS:只兼容IE9+,没法兼容火狐,谷歌,欧朋的内核。能够兼容的请多多指教。邮箱地址:onestopweb#163.com。
效果图: