【发布时间】:2014-08-02 01:11:05
【问题描述】:
我想获取一些 JSON 值并从中生成变量,但是当我这样做时出现错误。
在第一阶段 JSON 数组为空,这就是我使用 if != null 的原因,但即使使用填充数组我也会收到错误。
var tempJS=[];
$("#sth td").each(function(){
$this = $(this);
tempJS.push({"COLOR":$this.attr("data-color"),});
});
console.log(JSON.stringify(tempJS));
if(tempJS!=null) var kolor=tempJS[c-1].COLOR;
为什么最后一行给我以下错误:
未捕获的类型错误:无法读取未定义的属性“颜色”
【问题讨论】:
-
错误非常明确,未定义的是 tempJS[c-1],代码上的“c”在哪里?,除了你检查 tempJS 不为空,它分配 tempJS[ c-1],你应该检查同样的事情
标签: javascript json