JavaScript實(shí)現(xiàn)復(fù)制文章自動(dòng)添加版權(quán)
第一種
<script type="text/javascript">
document.body.oncopy = function(){
setTimeout(
function (){
var text = clipboardData.getData("text");
if(text){
text = text + "\r\n本文來自: (www.dhdzp.com) 詳細(xì)出處參考:"+location.href; clipboardData.setData("text", text);
}
},100)
}
</script>
注意:這段代碼必須復(fù)制到 body 區(qū)域里面才能生效,放到 head 區(qū)域內(nèi)是不起作用的。
第二種
$("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);
});
總結(jié)
以上就是小編為大家整理的兩種利用JavaScript實(shí)現(xiàn)復(fù)制文章自動(dòng)添加版權(quán)的方法,代碼很簡(jiǎn)單,有需要的朋友們可以參考學(xué)習(xí)。
- js 復(fù)制或插入Html的實(shí)現(xiàn)方法小結(jié)
- Javascript 實(shí)現(xiàn)復(fù)制(Copy)動(dòng)作方法大全
- 多瀏覽器兼容性比較好的復(fù)制到剪貼板的js代碼
- javascript 三種數(shù)組復(fù)制方法的性能對(duì)比
- javascript復(fù)制對(duì)象使用說明
- 用 javascript 實(shí)現(xiàn)的點(diǎn)擊復(fù)制代碼
- 網(wǎng)站內(nèi)容禁止復(fù)制和粘貼、另存為的js代碼
- 兼容IE與Firefox的js 復(fù)制代碼
- 兼容主流瀏覽器的JS復(fù)制內(nèi)容到剪貼板
- JS input文本框禁用右鍵和復(fù)制粘貼功能的代碼
相關(guān)文章
微信小程序日期增加時(shí)間完成訂單失效倒計(jì)時(shí)效果
這篇文章主要介紹了微信小程序日期增加時(shí)間完成訂單失效倒計(jì)時(shí)效果,在我們?nèi)粘Y?gòu)物過程中經(jīng)常會(huì)遇到這樣的功能,本文通過示例代碼給大家詳細(xì)講解,需要的朋友參考下吧2024-04-04
微信小程序配置視圖層數(shù)據(jù)綁定相關(guān)示例
這篇文章主要為大家介紹了微信小程序配置視圖層數(shù)據(jù)綁定相關(guān)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪<BR>2022-04-04
使用javascript做的一個(gè)隨機(jī)點(diǎn)名程序
這篇文章主要介紹了使用javascript做的一個(gè)隨機(jī)點(diǎn)名程序,經(jīng)測(cè)試,效果相當(dāng)不錯(cuò),需要的朋友可以參考下2014-02-02
JS實(shí)現(xiàn)超精簡(jiǎn)的鏈接列表在固定區(qū)域內(nèi)滾動(dòng)效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)超精簡(jiǎn)的鏈接列表在固定區(qū)域內(nèi)滾動(dòng)效果代碼,非常常見的頁(yè)面元素屬性變換控制實(shí)現(xiàn)滾動(dòng)效果,簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-11-11
JavaScript?顯示一個(gè)倒計(jì)時(shí)廣告牌的實(shí)現(xiàn)示例
本文主要介紹了JavaScript?顯示一個(gè)倒計(jì)時(shí)廣告牌的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
JavaScript實(shí)現(xiàn)頁(yè)面截圖3種解決方案
網(wǎng)頁(yè)截圖是指將網(wǎng)頁(yè)上的內(nèi)容截取下來,并保存為圖片的過程,下面這篇文章主要給大家介紹了關(guān)于JavaScript實(shí)現(xiàn)頁(yè)面截圖的3種解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06

