Home >Web Front-end >JS Tutorial >用js+xml自动生成表格的东西_javascript技巧

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

WBOY
WBOYOriginal
2016-05-16 19:23:02856browse
复制代码 代码如下:




无标题文档
<script> <BR>function loadXML(handler) { <BR> var url = "employees.xml"; <BR> if(document.implementation&&document.implementation.createDocument) { <BR> var xmldoc = document.implementation.createDocument("", "", null); <BR> xmldoc.onload = handler(xmldoc, url); <BR> xmldoc.load(url); <BR> } <BR> else if(window.ActiveXObject) { <BR> var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); <BR> xmldoc.onreadystatechange = function() { <BR> if(xmldoc.readyState == 4) handler(xmldoc, url); <BR> } <BR> xmldoc.load(url); <BR> } <BR>} <BR>function makeTable(xmldoc, url) { <BR> var table = document.createElement("table"); <BR> table.setAttribute("border","1"); <BR> table.setAttribute("width","600"); <BR> table.setAttribute("class","tab-content"); <BR> document.body.appendChild(table); <BR> var caption = "Employee Data from " + url; <BR> table.createCaption().appendChild(document.createTextNode(caption)); <BR> var header = table.createTHead(); <BR> var headerrow = header.insertRow(0); <BR> headerrow.insertCell(0).appendChild(document.createTextNode("姓名")); <BR> headerrow.insertCell(1).appendChild(document.createTextNode("职业")); <BR> headerrow.insertCell(2).appendChild(document.createTextNode("工资")); <BR> var employees = xmldoc.getElementsByTagName("employee"); <BR> for(var i=0;i<employees.length;i++) { <BR> var e = employees[i]; <BR> var name = e.getAttribute("name"); <BR> var job = e.getElementsByTagName("job")[0].firstChild.data; <BR> var salary = e.getElementsByTagName("salary")[0].firstChild.data; <BR> var row = table.insertRow(i+1); <BR> row.insertCell(0).appendChild(document.createTextNode(name)); <BR> row.insertCell(1).appendChild(document.createTextNode(job)); <BR> row.insertCell(2).appendChild(document.createTextNode(salary)); <BR> } <BR>} <BR></script>







复制代码 代码如下:



    
        Programmer
        32768
    

    
        Sales
        70000
    

    
        CEO
        100000
    


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn