将复制word中内容中的垃圾代码自动去掉

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 hanghwp 的 blog

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>

<body>
<script>
function getclipboardhtml()
{
var odiv = document.getelementbyid("divtemp")
odiv.innerhtml = "" ;

var otextrange = document.body.createtextrange() ;
otextrange.movetoelementtext(odiv) ;
otextrange.execcommand("paste") ;

var sdata = odiv.innerhtml ;
odiv.innerhtml = "" ;

return sdata ;
}


function cleanandpaste( html )
{
// remove all span tags
html = html.replace(/<\/?span[^>]*>/gi, "" );
// remove class attributes
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// remove style attributes
html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
// remove lang attributes
html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// remove xml elements and declarations
html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
// remove tags with xml namespace declarations: <o:p></o:p>
html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
// replace the &nbsp;
html = html.replace(/&nbsp;/, " " );
// transform <p> to <div>
var re = new regexp("(<p)([^>]*>.*?)(<\/p>)","gi") ;// different because of a ie 5.0 error
html = html.replace( re, "<div$2</div>" ) ;

//inserthtml( html ) ;

//return html;

form1.my.value = html;
}

function paste()
{
var shtml = getclipboardhtml() ;

var re = /<\w[^>]* class="?msonormal"?/gi ;
var re2 = /<\w[^>]* class="?xl"?/gi ;
if ( re.test( shtml ) )
{
alert("您要粘贴的内容好像是来自 ms word,系统将清除 ms word 格式后再粘贴!")
}

if ( re2.test( shtml ) )
{
alert("您要粘贴的内容好像是来自 ms excel,系统将清除 ms excel 格式后再粘贴!")
}
cleanandpaste( shtml ) ;
}
</script>
<form id="form1" name="form1">
<input type="button" value="paste" onclick="paste()">
<br/>
<br/>
<textarea name="my" cols="60" rows="10"></textarea>
<div id=divtemp
style="visibility: hidden; overflow: hidden; width: 1px; position: absolute; height: 1px"></div>
</form>
</body>
</html>

本文关键:将复制word中内容中的垃圾代码自动去掉
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top