把Work生成的HTML中的代码进行简化处理
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WORD转HTMLtitle>
<style>
body{ font-size:12px; padding:20px; margin:0px;}
p{ font-size:12px; margin:0px; line-height:25px; padding:0px;}
table{ border-left:1px solid #CCCCCC; border-top:1px solid #CCCCCC;}
table td{ border-right:1px solid #CCCCCC; border-bottom:1px solid #CCCCCC;}
style>
<script language="javascript" type="text/javascript">
//运行文本域代码
function runEx(cod1) {
cod=document.all(cod1)
var code=cod.value;
if (code!=""){
var newwin=window.open('','',''); //打开一个窗口并赋给变量newwin。
newwin.opener = null // 防止代码对论谈页面修改
newwin.document.write(code); //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
newwin.document.close();
}
}
script>
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 all B tags
html = html.replace(/<\/?B[^>]*>/gi, "" );
// Remove all COLGROUP tags
html = html.replace(/<\/?COLGROUP[^>]*>/gi, "" );
html = html.replace(/<\/?COL[^>]*>/gi, "" );
// Remove all td tags
html = html.replace(/]*>/gi, " " );
html = html.replace(/TD/gi, "td" );
html = html.replace(/TR/gi, "tr" );
html = html.replace(/TABLE/gi, "table" );
html = html.replace(/<\/?TBODY[^>]*>/gi, "" );
// Remove all tr tags
html = html.replace(/]*>/gi, "" );
// Remove all table tags
html = html.replace(/]*>/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:
html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
// Replace the
html = html.replace(/ /, "" );
//replace H to
html = html.replace( //gi
, "" ) ;
html = html.replace( /<\/H1>/gi, "" ) ;
html = html.replace( //gi
, "" ) ;
html = html.replace( /<\/H2>/gi, "" ) ;
html = html.replace( //gi
, "" ) ;
html = html.replace( /<\/H3>/gi, "" ) ;
//删除font
html = html.replace( /<(font[^>]*)>/gi, "" ) ;
html = html.replace( /<\/font>/gi, "" ) ;
//html = html.replace(/<\/?A[^>]*>/gi, "" );
// Transform to
//把td里面的p去除
var re = new RegExp("([^>]*]*>.*?)(<\/P><\/td>)","gi") ;
// Different because of a IE 5.0 error
html = html.replace( re, "
" ) ;
//删除p空格
html = html.replace(/<(p[^>]*)><\/p>/gi, "");
html = html.replace(/<(p[^>]*)> <\/p>/gi, "");
//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("您要粘贴的内容好像是来自 Word,系统将清除 Word 格式后再粘贴!")
}
if ( re2.test( sHTML ) )
{
alert("您要粘贴的内容好像是来自 Excel,系统将清除 Excel 格式后再粘贴!")
}
cleanAndPaste( sHTML ) ;
}
script>
<Form id="Form1" name="Form1">
<p>点击粘贴p>
<input type="button" value="粘贴" onclick="Paste()">
<br/>
<textarea name="my" cols="60" rows="10" id="rn01">textarea>
<br/>
<INPUT onclick="runEx('rn01')" type="button" value="运行代码" style="cursor:hand">
<div id=divTemp style="VISIBILITY: hidden; OVERFLOW: hidden; WIDTH: 1px; POSITION: absolute; HEIGHT: 1px">div>
Form>
BODY>
HTML>
你可能感兴趣的:(JavaScript)