我有一个数据对象,里面有带有 json 数据的数据对象。这是看起来的样子。文件名是data.js
var data =
[
{
title: 'this is title one',
commit: 'this is commit'
},
{
title: 'this is title two',
commit: 'this is commit'
},
{
title: 'this is title three',
commit: 'this is commit'
},
{
title: 'this is title four',
commit: 'this is commit'
},
{
title: 'this is title five',
commit: 'this is commit'
},
{
title: 'this is title six',
commit: 'this is commit'
},
{
title: 'this is title seven',
commit: 'this is commit'
}
]
这是我的代码
for (var i = 0; i < data.length; i++) {
$(container).load(view, function () {
console.log(data[i].title);
li.append('<img src="../images/profile.png" alt="Profile photo" />');
li.append('<span class="commit">' + data[i].title + '</span>');
li.append('<p><a href="#" class="comment">' + data.commit + '</a></p>');
li.append('<section class="clear"></section>');
$('#home ul').append(li);
});
}
现在我在控制台中收到此错误
Cannot read property 'title' of undefined
上面的错误在这一行
li.append('<span class="commit">' + data[i].title + '</span>');
如何解决这个问题?