【发布时间】:2010-01-07 21:14:03
【问题描述】:
如何将变量分配给已使用 .replace() 操作的返回 JSON 数据,如下所示。 目的:根据 JSON 返回的视频 ID 构建视频缩略图的 URL。
问题:如何将视频 ID(例如 mVWhWsgHzKM)分配给变量,以便构建缩略图 URL。
$.getJSON(
'http://gdata.youtube.com/feeds/users/soccerdude1935/favorites?alt=json-in-script&callback=?',
function(data){
$.each(data.feed.entry, function(i, item){
// assign variable to the returned JSON data
var id = item['id']['$t'];
// URL contain video ID is put inside a P tag and assign class ID.
$("<p></p>").text(id).addClass('vidId');
$('p.vidId').each(function () {
var id = $(this).text();
// removes all other text from URL except for video ID.
id = id.replace("http://gdata.youtube.com/feeds/videos/","");
// video ID is assigned to a var. Is this correct? because it is not working as a var.
// Note: no errors when running the code.
var imgSrc = $(this).text(id);
});
// thumbnail URL construction and placement inside a img element's src tag.
$(<img />).attr('src', 'http://i2.ytimg.com/vi/'+imgSrc+'/default.jpg');
});
});
导致 img src URL 看起来像:http://i2.ytimg.com/vi/mVWhWsgHzKM/default.jpg 但是当我运行该代码时,它不会呈现所需的结果。有什么建议吗?
任何想法将不胜感激。谢谢。
【问题讨论】:
标签: javascript jquery json string variables