JS将复制的页面内容以纯文本的形式粘贴到自己的页面上

版权声明:本文的原作者 http://blog.csdn.net/mooncom/article/ ,支持原著,拒绝盗版。 https://blog.csdn.net/MOONCOM/article/details/73722242

需求:从网上复制网页内容,通过页面(不是通过代码)粘贴到自己开发的页面上时,消除原来内容的所有样式,只粘贴纯文本。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
</head>

<body>  
<div ID="demo" contenteditable onpaste="setpaste(this)">

</div> 

</body>
<style>
	#demo{
		height: 400px;
		border:1px solid black;
	}
</style>
<script type="text/javascript">
function setpaste(item){
	window.setTimeout("change("+item.id+")",10);
}

function change(item){

	var new_content = item.innerHTML;
	var new_content = new_content.replace(/class="[^"]+"/ig, '');
   
   new_content = new_content.replace(/class\="[^"]+"/gi, '');
   
   new_content = new_content.replace(/<h1.*?>(.*?)<\/h1>/ig,"$1");
    
   new_content = new_content.replace(/<h2.*?>(.*?)<\/h2>/ig,"$1");
    
   new_content = new_content.replace(/<h3.*?>(.*?)<\/h3>/ig,"$1");
    
   new_content = new_content.replace(/<h4.*?>(.*?)<\/h4>/ig,"$1");
    
   new_content = new_content.replace(/<h5.*?>(.*?)<\/h5>/ig,"$1");
    
   new_content = new_content.replace(/<h6.*?>(.*?)<\/h6>/ig,"$1");
    
   new_content = new_content.replace(/style\="[^"]+"/gi, '');

   new_content = new_content.replace(/<\/?strong>/gi,'');
   item.innerHTML=new_content;
}
</script> 
</html>
展开阅读全文

没有更多推荐了,返回首页