文章目录[隐藏]
示例代码
//监听ctrl+v 复制 document.addEventListener("copy", function(e) { alert('复制成功,请遵循本文的转载规则'); }); //把监听提示添加到文章内容中 //文章内容div的ID $('#Inside_content').on('copy', function(e) { // IE8 or earlier browser is 'undefined' if (typeof window.getSelection === 'undefined') return; var selection = window.getSelection(); // if the selection is short let's not annoy our users. if (('' + selection).length < Number.parseInt('120')) { return; } //在内容末尾追加提示内容 var bodyElement = document.getElementsByTagName('body')[0]; var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; bodyElement.appendChild(newdiv); newdiv.appendChild(selection.getRangeAt(0).cloneContents()); //格式化 if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') { newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>"; } var url = document.location.href; //追加的内容 newdiv.innerHTML += '<br />' + '来源: 陌涛小站<br />' + '文章作者: 陌涛<br />' + '文章链接: <a href="'%20+%20url%20+%20'">'%20+%20url%20+%20'</a><br />' + '本文章著作权归作者所有,任何形式的转载都请注明出处。'; selection.selectAllChildren(newdiv); window.setTimeout(function() { bodyElement.removeChild(newdiv); }, 200); });
发表回复