DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> Ajax 從XML到生成表格
Ajax 從XML到生成表格
編輯:AJAX詳解     
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xHtml1-strict.dtd">
<html XMLns="http://www.w3.org/1999/xHtml">
<head>
<title>Dynamically Editing Page Content</title>
<script. type="text/Javascript">
var XMLHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
function DOSearch() {
    createXMLHttpRequest();
    XMLHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", "dynamicContent.XML", true);
    XMLHttp.send(null);
}
function handleStateChange() {
    if(XMLHttp.readyState == 4) {
        if(XMLHttp.status == 200) {
            clearPreviousResults();
            parseResults();
        }
    }
}
function clearPreviousResults() {
    var header = document.getElementById("header");
    if(header.hasChildNodes()) {
        header.removeChild(header.childNodes[0]);
    }
   var tableBody = document.getElementById("resultsBody");
    while(tableBody.childNodes.length > 0) {
        tableBody.removeChild(tableBody.childNodes[0]);
    }
}
function parseResults() {
    var results = xmlHttp.responseXML;
    var property = null;
    var address = "";
    var price = "";
    var comments = "";
    var propertIEs = results.getElementsByTagName("property");
    for(var i = 0; i < propertIEs.length; i++) {
        property = propertIEs[i];
        address = property.getElementsByTagName("address")[0].firstChild.nodeValue;
        price = property.getElementsByTagName("price")[0].firstChild.nodeValue;
        comments = property.getElementsByTagName("comments")[0].firstChild.nodeValue;
        addTableRow(address, price, comments);
    }
    var header = document.createElement("h2");
    var headerText = document.createTextNode("Results:");
    header.appendChild(headerText);
    document.getElementById("header").appendChild(header);
    document.getElementById("resultsTable").setAttribute("border", "1");
}
function addTableRow(address, price, comments) {
    var row = document.createElement("tr");
    var cell = createCellWithText(address);
    row.appendChild(cell);
    cell = createCellWithText(price);
    row.appendChild(cell);
    cell = createCellWithText(comments);
    row.appendChild(cell);
    document.getElementById("resultsBody").appendChild(row);
}
function createCellWithText(text) {
    var cell = document.createElement("td");
    var textNode = document.createTextNode(text);
    cell.appendChild(textNode);
    return cell;
}
</script>
</head>
<body>
<form>
    <input type="button" value="Search" nclick="DOSearch();"/>   
</form>
<span id="header">
</span>
<table id="resultsTable" width="75%" border="0">
    <tbody id="resultsBody">
    </tbody>
</table>
</body>
</Html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved