在自己的个人博客中实现复制粘贴带小尾巴的功能

实现复制粘贴小尾巴的功能:只要在文章的最后引入一下代码就可以实现

<script>
$("body").bind('copy', function (e) {
	if (typeof window.getSelection == "undefined") return; //IE8 及更老的版本不兼容
	
	var body_element = document.getElementsByTagName('body')[0];
	var selection = window.getSelection();
	
	//如果选择是复制是低于50个字符的内容,是不会带小尾巴的
	if (("" + selection).length < 50) return;
 
	//创建一个DIV的可见区域之外
	//并填写选定的文本
	var newdiv = document.createElement('div');
	newdiv.style.position = 'absolute';
	newdiv.style.left = '-99999px';
	body_element.appendChild(newdiv);
	newdiv.appendChild(selection.getRangeAt(0).cloneContents());
	
	//我们需要<pre>标签解决方案
	//其他的文本在<pre>失去了所有的行符!
	if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") {
		newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
	}
	
	newdiv.innerHTML += "<br /><br />文章来自: 网站名称()  详文参考:<a href='"
	+ document.location.href + "'>"
	+ document.location.href + "</a>";
			
	selection.selectAllChildren(newdiv);
	window.setTimeout(function () { body_element.removeChild(newdiv); }, 200);
});
</script>

测试地址:http://www.aijquery.cn/Html/ShiLi/84.html

发布了104 篇原创文章 · 获赞 574 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/LQZ8888/article/details/103068988