清理由Word导出的HTML文档的JS脚本

来源:互联网 发布:ios10软件源大全 编辑:程序博客网 时间:2024/05/25 13:33

当把一个Word文档导出成HTML格式时,会发现到处的文件之中会有很多样式,结果使得文件很大。下面的这个脚本可以清理其中的样式信息,只保留数据信息。

使用方法:

把<script src="ParseHtml.js" language="javascript"></script>放到html文件的<head>中,然后打开文件,会看到在页面的最下面多出了一个按钮。点击,弹出一个新的页面,并提示保存文件。保存即可。

说明:因为时间比较仓卒,所以可能会有一些错误,请大家帮忙指正。

//<script src="ParseHtml.js" language="javascript"></script>function Parse(root){ var html=""; var tag; var value; for(var i=0;i<root.childNodes.length;i++)   {    if(root.childNodes[i].id=="QLSC_MUMU_ButtonParse")   continue;     tag=root.childNodes[i].tagName;  value=root.childNodes[i].nodeValue;  if((tag+"").toLowerCase()=="font")   html+=Parse(root.childNodes[i]);  if(tag+""=="undefined")   html+=value;  else  {   if(root.childNodes[i].childNodes.length>0)   {    html+="<"+tag+">";    if(value!=null)     html+=value;    html+=Parse(root.childNodes[i]);    html+="</"+tag+">";   }   else   {    html+="<"+tag+">";    if(value!=null)    {     if(value!=null)     {      html+=value;      html+="</"+tag+">";     }       }    else    {     tmp=(tag+"").toLowerCase();     if(tmp=="script"||tmp=="title"||tmp=="link"||tmp=="meta"||tmp=="style")         html+="</"+tag+">";    }   }  } } return html;}

function Start(){ var ret=Parse(document.documentElement); var wnd=window.open("","mumu","addressbar=no,toolbar=yes,scrollbars=yes,resizable=yes",true);  wnd.document.write("<html>"+ret+"</html>");  wnd.document.close();  wnd.document.execCommand('SaveAs');}function Init(){ var body=document.getElementsByTagName("body").item(0); var node=document.createElement("input");

 node.setAttribute("value","Parse"); node.setAttribute("type","button"); node.setAttribute("id","QLSC_MUMU_ButtonParse");  body.appendChild(node); document.getElementById("QLSC_MUMU_ButtonParse").onclick=Start;}window.onload=Init;