JavaScript實現複製文章自動添加著作權_javascript技巧

來源:互聯網
上載者:User

第一種

<script type="text/javascript"> document.body.oncopy = function(){   setTimeout(     function (){     var text = clipboardData.getData("text");     if(text){       text = text + "\r\n本文來自: (www.jb51.net) 詳細出處參考:"+location.href; clipboardData.setData("text", text);     }   },100) } </script> 

注意:這段代碼必須複製到 body 地區裡面才會生效,放到 head 地區內是不起作用的。

第二種

$("body").bind('copy', function (e) { if (typeof window.getSelection == "undefined") return; //IE8 or earlier...  var body_element = document.getElementsByTagName('body')[0]; var selection = window.getSelection();  //if the selection is short let's not annoy our users if (("" + selection).length < 30) return; //create a div outside of the visible area //and fill it with the selected text var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; body_element.appendChild(newdiv); newdiv.appendChild(selection.getRangeAt(0).cloneContents());  //we need a <pre> tag workaround //otherwise the text inside "pre" loses all the line breaks! if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") { newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>"; }  newdiv.innerHTML += "<br /><br />Read more at: <a href='" + document.location.href + "'>" + document.location.href + "</a> © MySite.com";   selection.selectAllChildren(newdiv); window.setTimeout(function () { body_element.removeChild(newdiv); }, 200);});

總結

以上就是小編為大家整理的兩種利用JavaScript實現複製文章自動添加著作權的方法,代碼很簡單,有需要的朋友們可以參考學習。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.