Firefox是3(不正确), IE是1(正确)
我的问题是:怎么让Firefox 也是1呢?
======================<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null; Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
}; Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
//IE显示1
//Firefox显示3
//怎么让它俩一致呢?
alert(oRoot.childNodes.length);
}
};
//oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml");
</script>
</body>
</html>======================================
country.xml<?xml version='1.0' encoding='utf-8'?>
<root>
<country id="1" pid="0">中国
<municipality id="2" pid="1">北京</municipality>
<municipality id="3" pid="1">上海</municipality>
<province id="4" pid="1">辽宁
<city id="5" pid="4">沈阳</city>
<city id="6" pid="4">大连</city>
</province>
<municipality id="7" pid="1">天津</municipality>
</country>
</root>
我的问题是:怎么让Firefox 也是1呢?
======================<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null; Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
}; Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
//IE显示1
//Firefox显示3
//怎么让它俩一致呢?
alert(oRoot.childNodes.length);
}
};
//oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml");
</script>
</body>
</html>======================================
country.xml<?xml version='1.0' encoding='utf-8'?>
<root>
<country id="1" pid="0">中国
<municipality id="2" pid="1">北京</municipality>
<municipality id="3" pid="1">上海</municipality>
<province id="4" pid="1">辽宁
<city id="5" pid="4">沈阳</city>
<city id="6" pid="4">大连</city>
</province>
<municipality id="7" pid="1">天津</municipality>
</country>
</root>
要不就遍历所有结点看nodeType是多少来区分TextNode,要不就是用getElementsByTagName来找到想要的结点,然后访问length
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Content-Language" content="zh-cn"/>
<title>xml</title>
</head><body>
<script type="text/javascript">
function createDocument() {
if (window.ActiveXObject) {
var aVersions = [
"MSXML2.DOMDocument.6.0",
"MSXML2.DOMDocument.3.0",
"Microsoft.XmlDom"
]; for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlDom = new ActiveXObject(aVersions[i]);
return oXmlDom;
} catch (oError) {
//Do nothing
}
}
throw new Error("MSXML is not installed.");
} else if (document.implementation && document.implementation.createDocument) {
Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null;
Document.prototype._changeReadyState = function(iReady) {
this.readyState = iReady;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
};
Document.prototype._load = Document.prototype.load;
Document.prototype.load = function(sURL) {
this._changeReadyState(1);
this.load(sURL);
};
var oXmlDom = document.implementation.createDocument("", "", null);
oXmlDom.addEventListener("load", function() {
this._changeReadyState(4);
}, false);
return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//end createDocument var oXmlDom = createDocument();
oXmlDom.onreadystatechange = function () {
if (oXmlDom.readyState == 4) {
var oRoot = oXmlDom.documentElement;
var oNodes = oRoot.childNodes;
var n = 0;
for(var i=0;i<oNodes.length;i++) if(oNodes[i].nodeType==1) n++;
alert(n);
}
};
//oXmlDom.validateOnParse = false;
oXmlDom.load("country.xml");
</script>
</body>
</html>