$.ajax({
type : "get",
url : "http://127.0.0.1:8080/html/tables.txt",
dataType : "json",
success : function(json) {
$("a").click(function() {
for (var i = 0; i <= json.data.length; i++) {
if ($(this).attr("name") == json.data[i].name) {
//属于this name(这个name是动态添加的)会等于json的name传过来的值,这里面是可以执行的但不知道为什么会上面会报错
}
}
});
});
他报的错是
Uncaught TypeError: Cannot read property 'name' of undefined
改为
不要等号,有等号的话,
json.data[json.data.length]
会返回undefined
,因为找不到。建议以后写这种遍历整个数组的时候多用用
for in
语句或者是数组的forEach
方法。