无法读取属性'getElementsByTagName'为null,发生了什么?
问题描述:
我的问题似乎存在于LoadXML的子功能调用之间。看起来xml数据由于某种奇怪的原因而变为null,我不知道如何解决这个问题。 Stackexchange似乎有许多类似的问题,但很多都没有回答,或者他们的回答没有帮助我的案子。无法读取属性'getElementsByTagName'为null,发生了什么?
function load() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
var idname = document.getElementById("name").value;
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this); //it obtains it here...
LoadXML(this, idname);
}
};
xmlhttp.open("GET", "helper_database.xml", false);
xmlhttp.overrideMimeType('text/xml');
xmlhttp.send();
}
function LoadXML(xml, name) {
var x, i, xmlDoc, nametxt, areEqual;
xmlDoc = xml.responseXML;
nametxt = name;
console.log("HERE \n" + xmlDoc); //...but it becomes null.
x = xmlDoc.getElementsByTagName("name"); //this returns the error of "Cannot read property 'getElementsByTagName' of null"
console.log("muuttujan x eka: " + x[0]);
for (i = 0; i < x.length; i++) {
if (areEqual = xmlDoc.getElementsByTagName("name").toUpperCase() === nametxt.toUpperCase()) {
document.getElementById("ComFocus").value = x[i];
}
}
}
这里是helper_database.xml
<Character>
<name>test</name>
<stats>
<Com>1</Com>
<Con>2</Con>
<Cun>3</Cun>
<Dex>4</Dex>
<Mag>5</Mag>
<Per>6</Per>
<Str>7</Str>
<Wil>8</wil>
</stats>
</Character>
答
你有一些类型0以及一些分析错误。
需要注意的是:
-
getElementsByTagName().toUpperCase
是无效的,因为gEBTN返回对象的数组。所以,你必须使用getElementsByTagName()[i].innerHTML.toUpperCase()
。 - 而不是使用
console.log("muuttujan x eka: " + x[0]);
,使用console.log("muuttujan x eka: " + x[0].innerHTML);
function load() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
var idname = document.getElementById("name").value;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp); //it obtains it here...
LoadXML(xmlhttp, idname);
}
};
xmlhttp.open("GET", "helper_database.xml", false);
//xmlhttp.overrideMimeType('text/xml');
xmlhttp.send();
}
function LoadXML(xml, name) {
var x, i, xmlDoc, nametxt, areEqual;
xmlDoc = xml.responseXML;
nametxt = name;
console.log("HERE \n" + xmlDoc); //...but it becomes null.
x = xmlDoc.getElementsByTagName("name"); //this returns the error of "Cannot read property 'getElementsByTagName' of null"
console.log("muuttujan x eka: " + x[0].innerHTML);
for (i = 0; i < x.length; i++) {
if (areEqual = xmlDoc.getElementsByTagName("name")[0].innerHTML.toUpperCase() === nametxt.toUpperCase()) {
document.getElementById("ComFocus").value = x[i];
}
}
}
<html>
<body>
<input id="name" onblur="load();" />
<div id="ComFocus"></div>
</body>
</html>
XML
<?xml version="1.0" encoding="UTF-8"?>
<Character><name>test</name>
<stats>
<Com>1</Com>
<Con>2</Con>
<Cun>3</Cun>
<Dex>4</Dex>
<Mag>5</Mag>
<Per>6</Per>
<Str>7</Str>
<Wil>8</Wil>
</stats></Character>
这只是一个错字(我很惊讶,你没有得到的错误在Web控制台中,但是我也没有):XML格式错误:''请注意,结束标签是'wil',而不是''。解决这个问题,它解析并且填入了'responseXML'。 –
我不建议用'console.log'手电筒在黑暗中徘徊,而是建议使用内置在浏览器中的全功能调试器打开灯光。使用它,你可以在你的'onreadystatechange'处理器中查看'this',并且看到它的'responseXML'属性是'null'。 –
像T.J. Crowder写道它是XML文件中的一种类型。此外,你会在平等支票上遇到问题。 'xmlDoc.getElementsByTagName(“name”)'是一个集合,所以不会有'.toUpperCase()'。你必须做类似'xmlDoc.getElementsByTagName(“name”)[i] .innerHTML.toUpperCase()'或更短'x [i] .innerHTML.toUpperCase()' –