2011-02-28 70 views
0

嗨,大家好,請你告訴我如何訪問audioCollection對象內的內容?jQuery訪問對象值

我使用Echonest jQuery插件使用jQuery模板

https://github.com/Rodeoclash/Echonest-jQuery-Plugin/blob/master/scripts/echonest.js

我去螢火蟲和類型化console.log(audioCollection)和我得到ReferenceError: audioCollection is not defined。不知道我是否做得對。

echonest.artist(artist).audio(function(audioCollection) { 

$('#blog-page').find('.post').append(audioCollection.to_html('<p class="song-url" style="display:none;">${url}</p>')); 
//appends url variable to html inside of audioCollection 

var testing = audioCollection; //returns [Object object] 
}); 

謝謝!

+0

你究竟在哪裏放了'console.log(audioCollection)'? – 2011-02-28 21:19:05

+0

你在使用一些插件嗎?如果是,哪一個? 'audio'不是標準的jQuery函數。你必須提供更多信息。 – 2011-02-28 21:19:31

+0

'audioCollection'只存在於其封閉函數的範圍內。這就是爲什麼你無法使用Firebug從全球範圍獲得它的價值。 – 2011-02-28 21:22:19

回答

2

我不熟悉的對象,但你可以嘗試使用我的dump()功能,看看裏面有什麼..

echonest.artist(artist).audio(function(audioCollection) { 

    $('#blog-page').find('.post').append(audioCollection.to_html('<p class="song-url" style="display:none;">${url}</p>')); 
    //appends url variable to html inside of audioCollection 

    var testing = audioCollection; //returns [Object object] 
    alert(dump(testing));   
}); 

function dump(arr,level) { 
    var dumped_text = ""; 
    if(!level) level = 0; 

    //The padding given at the beginning of the line. 
    var level_padding = ""; 
    for(var j=0;j<level+1;j++) level_padding += " "; 

    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
     for(var item in arr) { 
      var value = arr[item]; 

      if(typeof(value) == 'object') { //If it is an array, 
       dumped_text += level_padding + "'" + item + "' ...\n"; 
       dumped_text += dump(value,level+1); 
      } else { 
       dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; 
      } 
     } 
    } else { //Stings/Chars/Numbers etc. 
     dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; 
    } 
    return dumped_text; 
} 

UPDATE

訪問你的價值觀,你可以做這樣的事情

var songs = testing.audio; 
for (var x=0; x<songs.length; x++){ 
    alert(songs[x].title); 
} 
+0

嗨,它返回這個:http://sharepaste.com/p/iyKbGL我想知道如何訪問值。我很感激任何關於這個主題的閱讀 – Ronal 2011-02-28 21:34:21

1

我只想要一個原始視圖的對象,我建議廣泛使用JSON.stringify()

echonest.artist(artist).audio(function(audioCollection) { 
    console.log(JSON.stringify(audioCollection)); 

    // Appends url variable to html inside of audioCollection. 
    $('#blog-page').find('.post').append(audioCollection.to_html(
     '<p class="song-url" style="display:none;">${url}</p>')); 
});