gpt4 book ai didi

javascript - x[i].parentElement.NodeName 适用于除资源管理器之外的所有内容。为什么?

转载 作者:行者123 更新时间:2023-12-03 03:38:35 25 4
gpt4 key购买 nike

我正在从 XMLHttpRequest 解析 XML 文件:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
loadItems(this);
}
};
xhttp.open("GET", "articles.xml", true);
xhttp.send();

function loadItems(xml) {
var xmlDoc = xml.responseXML;
var items = xmlDoc.getElementsByTagName("item");
for(var i = 0; i < items.length; i++){
//Next line is where the script crashes...
var newCategory = articles[i].parentElement.nodeName;
alert(newCategory);
}
}

我已经找到了我的错误:

var newCategory = articles[i].parentElement.nodeName;

我正在使用的 XML 结构:

<head>
<category>
<item>
<title>
foo
</title>
<contents>
bar
</contents>
</item>
</category>
<newcategory>
<item>
<title>
left
</title>
<contents>
right
</contents>
</item>
</newcategory>
</head>

这似乎适用于除 Internet Explorer 之外的所有其他浏览器,其中脚本在这一行静默崩溃。在其他浏览器中,newCategory 将具有字符串“category”或“newcategory”,具体取决于每个项目的父节点。

我查看了 Windows 文档,他们说支持parentElement 和nodeName 访问器,那么为什么会使脚本在IE 中崩溃呢?

最佳答案

在对 Internet Explorer 文档进行一些研究后,我发现有一个可以在 Firefox/Chrome/Opera 和 Explorer 中使用的 ParentNode 访问器。

var newCategory = articles[i].parentNode.nodeName;

关于javascript - x[i].parentElement.NodeName 适用于除资源管理器之外的所有内容。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45739775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com