这是我的xml文件(simple.xml):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<firstLevelChild>first level child 1</firstLevelChild>
<firstLevelChild>first level child 2</firstLevelChild>
</root>
这是我的JavaScript:
<p><button onclick="doSomethingSimple()">do something simple</button></p>
<p id="demo">demo</p>
<script>
function doSomethingSimple(){
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
simpleFunction(xhttp);
}
};
xhttp.open("GET", "simple.xml", true);
xhttp.send();
}
function simpleFunction(xml){
var xmldoc = xml.responseXML;
var rootNode, firstChildOfRoot, textNode;
var outputText;
rootNode = xmldoc.documentElement;
firstChildOfRoot = rootNode.firstChild;
textNode = firstChildOfRoot.firstChild;
outputText = textNode.nodeValue;
//outputText = "hallo from 'simpleFunction()'";
document.getElementById("demo").innerHTML = outputText;
}
</script>
当我点击按钮时,没有任何反应。无法弄清楚为什么不。 请帮忙。 感谢。