我的基本要求是我可以得到一個YouTube視頻的標題,以便我可以訪問它並將其應用到我的頁面,但是我遇到了一個問題,我嘗試在評論中解釋:jQuery JSON沒有定義變量
//first I declare my array and variable for the video information
var videoID = [];
var videoTitle;
//this ensures that the <div> exists
if ($("#productVideo0").length > 0) {
//then for every occurrence of and embedded video...
$(".youtube-video").each(function() {
//I call this function which strips the video id out of the URL (in the array[1] element]
videoID = getVideoID($(this).children().attr('src'));
//then I make the JSON call
$.getJSON('http://gdata.youtube.com/feeds/api/videos/' + videoID[1] + '?v=2&alt=jsonc', function (data) {
//if I alert this line below, it displays the title.
//However, if I try to alert or use this variable outside the function
//it just displays "undefined" or [object,object] depending on what I do
videoTitle = data.data.title;
})
})
}
//this uses regex to strip the id from the url
function getVideoID(url) {
url = url.match(/embed\/(\w*)/);
return url;
}
我不能真正看到你在解析JSON。 – Bloodcount
而不是警報,使用控制檯,那麼你將能夠看到你的對象,而不是'[object Object]' –