1
我有以下函數,它使用Reddit的JSON API來列出來自subreddit「/ r/fffffffuuuuuuuuuuuu。」的最新Rage漫畫。我有它拉圖像URL並輸出它作爲一個IMG。但問題是,有時提交的漫畫使用鏈接到'imgur.com'上的圖像,而不是'i.imgur.com'上的直接鏈接。如果圖像不是圖像,會獲取圖像的URL?
我最初嘗試拆分請求的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
});
});
};