我正在尝试使用jQuery解析表并填充数组,我已编写此代码来执行此操作:
var comments = new Array();
$("#mytable tr:nth-child(odd)").each( function( index ) {
comments[index]["url"] = $(this).find(".menu2 a").attr("href");
comments[index]["name"] = $(this).find(".menu2 a").text();
comments[index]["avatar"] = $(this).find(".menu2 a img").attr("src");
comments[index]["date"] = $(this).find("td[valign] b:first-child").text();
$(this).find("td[valign] b:first-child").remove();
comments[index]["report"] = $(this).find("td[valign] .pcomment_report").attr("data-refid");
$(this).find("td[valign] .pcomment_report").remove();
comments[index]["comment"] = $(this).find("td[valign]").html();
});
但它让我回答:
TypeError:无法设置未定义的属性'url'
如何使此代码有效?
答案 0 :(得分:5)
该消息暗示comments[index]
未定义。您需要先创建它:
comments[index] = {};