获取javascript未定义类型错误
请帮助…尝试执行下面提到的功能,但web控制台始终显示 TypeError:xml.location.forecast[j]未定义 我可以在alert中打印值,但由于此错误,代码无法向浏览器提供输出。尝试在不同的位置初始化j,并使用不同的增量方法。如何通过此类型错误获取javascript未定义类型错误,javascript,jquery,highcharts,undefined,typeerror,Javascript,Jquery,Highcharts,Undefined,Typeerror,请帮助…尝试执行下面提到的功能,但web控制台始终显示 TypeError:xml.location.forecast[j]未定义 我可以在alert中打印值,但由于此错误,代码无法向浏览器提供输出。尝试在不同的位置初始化j,并使用不同的增量方法。如何通过此类型错误 Meteogram.prototype.parseYrData = function () { var meteogram = this,xml = this.xml,pointStart; if (!xml) { re
Meteogram.prototype.parseYrData = function () {
var meteogram = this,xml = this.xml,pointStart;
if (!xml) {
return this.error();
}
var j;
$.each(xml.location.forecast, function (i,forecast) {
j= Number(i)+1;
var oldto = xml.location.forecast[j]["@attributes"].iso8601;
var mettemp=parseInt(xml.location.forecast[i]["@attributes"].temperature, 10);
var from = xml.location.forecast[i]["@attributes"].iso8601;
var to = xml.location.forecast[j]["@attributes"].iso8601;
from = from.replace(/-/g, '/').replace('T', ' ');
from = Date.parse(from);
to = to.replace(/-/g, '/').replace('T', ' ');
to = Date.parse(to);
if (to > pointStart + 4 * 24 * 36e5) {
return;
}
if (i === 0) {
meteogram.resolution = to - from;
}
meteogram.temperatures.push({
x: from,
y: mettemp,
to: to,
index: i
});
if (i === 0) {
pointStart = (from + to) / 2;
}
});
this.smoothLine(this.temperatures);
this.createChart();
};
您正在尝试访问最后一个元素之后的元素。在继续之前,您可以检查是否存在j所指的元素:
Meteogram.prototype.parseYrData = function () {
var meteogram = this,
xml = this.xml,
pointStart;
if (!xml) {
return this.error();
}
var i = 0;
var j;
$.each(xml.location.forecast, function (i, forecast) {
j = Number(i) + 1;
if (!xml.location.forecast[j]) return;
var from = xml.location.forecast[i]["@attributes"].iso8601;
var to = xml.location.forecast[j]["@attributes"].iso8601;
});
};
在上一次迭代中,无法进行下一次预测。我正在解析从json传递的xml数据。很好,但是最后一个元素后面的元素应该是什么?当然,它还没有定义。你能不能对代码做些修改,并把它作为一个注释,彼得。我已经修改了代码,只是为了显示主要内容。