更改(不使用 jquery)这些 html/javascript 以与 ie9 一起使用的最佳方法是什么,
<html>
<body>
<div id="xmls">
<xml id="myXml">
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
</xml>
</div>
<!-- ...more html -->
</body>
</html>
然后用脚本阅读这个 html 部分
function readBooks() {
var doc = null;
try {
doc = document.getElementById("myXml").XMLDocument.documentElement.childNodes.length;
alert('msg =' + doc);
//i need child nodes to iterate and show values,
} catch (ex) {
alert("problems to load xml" + doc);
throw ex;
}
}
这些适用于 ie6、7、8。
但 ie9 不行。
the XMLDocument property is undefined,
谢谢,
最佳答案
以下内容在现代浏览器中似乎没问题
function readBooks() {
var doc;
try {
doc = document.getElementById("myXml")
alert('msg =' + doc.childNodes.length);
for (var i = 0; i < doc.childNodes.length; i++) {
if(doc.childNodes[i].tagName && doc.childNodes[i].tagName.toUpperCase() == "BOOKSTORE") {
for (var j = 0; j < doc.childNodes[i].childNodes.length; j++) {
if (doc.childNodes[i].childNodes[j].tagName && doc.childNodes[i].childNodes[j].tagName.toUpperCase() == "BOOK") {
var book = doc.childNodes[i].childNodes[j];
alert(book.getAttribute("category"));
alert(getNodeValue(book.getElementsByTagName("title")[0]));
alert(getNodeValue(book.getElementsByTagName("author")[0]));
alert(getNodeValue(book.getElementsByTagName("year")[0]));
alert(getNodeValue(book.getElementsByTagName("price")[0]));
}
}
}
}
} catch (ex) {
alert("problems to load xml" + doc);
throw ex;
}
}
function getNodeValue(node) {
if (node.text)
return node.text;
return node.innerHTML;
}
关于javascript使用ie9从文档中加载xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618431/