【发布时间】:2011-12-08 19:24:08
【问题描述】:
我有以下函数,它使用 Reddit 的 JSON api 从 subreddit "/r/fffffffuuuuuuuuuuu" 中列出最新的愤怒漫画。我让它拉图像 URL 并将其作为 img 输出。然而问题在于,有时提交的漫画使用“imgur.com”上的图片链接,而不是“i.imgur.com”上的直接链接。
我最初尝试拆分请求的 URL,所以我只得到了“/”之后的字符串,但问题仍然是它不包含图像类型(即“.png”、“.jpg”等) .
我的问题是,如何从 i.imgur.com 获取不是直接 img src 的 URL 的 img src?
从下面的代码中可以看出,我目前唯一的解决方案是如果 img 不存在则输出错误。
函数如下:
var i = -1;
function rage(data) {
var rageURL = data['data']['children'][i]['data']['url'];
$('#comic .rage-wrap').html('<img src="'+rageURL+'" />');
$('img').load(function() { $('.rage-wrap').fadeIn(400); });
$('img').error(function() { alert('whoops!'); });
};
function nextRage() {
$('.next-rage').live('click', function() {
$('.rage-wrap').fadeOut(400);
i++;
$.ajax({
type: "GET",
url: "http://www.reddit.com/r/fffffffuuuuuuuuuuuu/.json",
dataType: "jsonp",
jsonp: "jsonp",
success: rage
});
});
};
【问题讨论】: