用js+xml自动生成表格的东西

时间:2021-05-18

复制代码 代码如下:
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title>无标题文档</title>
<scriptlanguage="javascript">
functionloadXML(handler){
varurl="employees.xml";
if(document.implementation&&document.implementation.createDocument){
varxmldoc=document.implementation.createDocument("","",null);
xmldoc.onload=handler(xmldoc,url);
xmldoc.load(url);
}
elseif(window.ActiveXObject){
varxmldoc=newActiveXObject("Microsoft.XMLDOM");
xmldoc.onreadystatechange=function(){
if(xmldoc.readyState==4)handler(xmldoc,url);
}
xmldoc.load(url);
}
}
functionmakeTable(xmldoc,url){
vartable=document.createElement("table");
table.setAttribute("border","1");
table.setAttribute("width","600");
table.setAttribute("class","tab-content");
document.body.appendChild(table);
varcaption="EmployeeDatafrom"+url;
table.createCaption().appendChild(document.createTextNode(caption));
varheader=table.createTHead();
varheaderrow=header.insertRow(0);
headerrow.insertCell(0).appendChild(document.createTextNode("姓名"));
headerrow.insertCell(1).appendChild(document.createTextNode("职业"));
headerrow.insertCell(2).appendChild(document.createTextNode("工资"));
varemployees=xmldoc.getElementsByTagName("employee");
for(vari=0;i<employees.length;i++){
vare=employees[i];
varname=e.getAttribute("name");
varjob=e.getElementsByTagName("job")[0].firstChild.data;
varsalary=e.getElementsByTagName("salary")[0].firstChild.data;
varrow=table.insertRow(i+1);
row.insertCell(0).appendChild(document.createTextNode(name));
row.insertCell(1).appendChild(document.createTextNode(job));
row.insertCell(2).appendChild(document.createTextNode(salary));
}
}
</script>
<linkhref="css/style.css"rel="stylesheet"type="text/css">
</head>

<bodyonLoad="loadXML(makeTable)">
</body>
</html>

复制代码 代码如下:
<?xmlversion="1.0"encoding="gb2312"?>
<employees>
<employeename="J.Doe">
<job>Programmer</job>
<salary>32768</salary>
</employee>
<employeename="A.Baker">
<job>Sales</job>
<salary>70000</salary>
</employee>
<employeename="BigCheese">
<job>CEO</job>
<salary>100000</salary>
</employee>
</employees>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章