xml.php<?php
header("Content-Type:text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo "<flowers>\n";
echo "<flower>\n";
echo "<name>牡丹</name>\n";
echo "</flower>\n";
echo "<flower>\n";
echo "<name>玫瑰</name>\n";
echo "</flower>\n";
echo "</flowers>";
?>index.html:<script language="javascript">
var xmlHttp;
var xmlDoc;
if(window.ActiveXObject) {
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}else if(window.XMLHttpRequest) {
xmlHttp=new XMLHttpRequest();
}
xmlHttp.open("GET","xml.php",true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4 && xmlHttp.status==200) {
if(window.ActiveXObject) { //IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(xmlHttp.responseXML);
alert(xmlDoc.getElementsByTagName("flower").length); //为2
}else if(document.implementation && document.implementation.createDocument) { //FireFox
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load(xmlHttp.responseXML);
xmlDoc.onload=function() {
alert(xmlDoc.getElementsByTagName("flower").length); //为0 }
}
}
}
</script>加载PHP页面返回的XML文档,为什么在IE下分解xmlDoc得到的数组的长度为2(正确),而在FireFox下得到的数组长度为0呢?
header("Content-Type:text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo "<flowers>\n";
echo "<flower>\n";
echo "<name>牡丹</name>\n";
echo "</flower>\n";
echo "<flower>\n";
echo "<name>玫瑰</name>\n";
echo "</flower>\n";
echo "</flowers>";
?>index.html:<script language="javascript">
var xmlHttp;
var xmlDoc;
if(window.ActiveXObject) {
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}else if(window.XMLHttpRequest) {
xmlHttp=new XMLHttpRequest();
}
xmlHttp.open("GET","xml.php",true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4 && xmlHttp.status==200) {
if(window.ActiveXObject) { //IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(xmlHttp.responseXML);
alert(xmlDoc.getElementsByTagName("flower").length); //为2
}else if(document.implementation && document.implementation.createDocument) { //FireFox
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load(xmlHttp.responseXML);
xmlDoc.onload=function() {
alert(xmlDoc.getElementsByTagName("flower").length); //为0 }
}
}
}
</script>加载PHP页面返回的XML文档,为什么在IE下分解xmlDoc得到的数组的长度为2(正确),而在FireFox下得到的数组长度为0呢?
分就给二位吧。