0

データ オブジェクトがあり、その中に 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>');

これを修正するには?

4

2 に答える 2

0

ええと、data.jsは実際にはjsonではありません。

最初にload()からgetJSON()に切り替えます

$.getJSON(view, function(data) { })

次に行を削除します

var data =

jsonの内容を持っているだけです。jQueryはJSONを引数データに割り当てます。

于 2012-11-02T08:26:11.683 に答える