我在数组中分配了一个带数组的变量,即
var encounter_names = new Array();
encounters.encountersDB.each(function(encounter) { encounter_names.push(new Array(encounter.key, encounter.name))})
这是我的for循环语句,如下所述
for (var i = 0; i < encounter_names.length; i++) {
window['li' + i] = $("li[key='" + encounter_names[i][0] + "']")
window['li' + i] = $("<li key='" + encounter_names[i][0] + "' class='list'><a class='arrow_'+ encounter_names[i][0] +'" + encounter_names[i][0] + "' href='#'>" + encounter_names[i][1] + "</a></li>")
$('.arrow_' + encounter_names[i][0] + '').click(function (event) {
encounters.show(window['key' + i]);
event.preventDefault();
})
encounters_list.append(encounter_names[i][0])
}
当我点击href链接时,我在控制台日志中收到以下错误:
TypeError: Result of expression 'encounter_names[i]' [undefined] is not an object.
欢迎任何建议。
答案 0 :(得分:1)
我认为遇见.encountersDB是一个对象,而不是一个html对象? 问题是:每个函数都有两个参数:当前索引和元素。所以你只需将其更改为:
var encounter_names = []; // short form for new Array()
$.each(encounters.encountersDB, function(index, encounter) {
encounter_names.push([encounter.key, encounter.name]);
})