我使用'href': article.data.url,
(例如:http://i.imgur.com/eUlT0yz.jpg)
但有时我会在没有 .jpg 扩展名的情况下获得这些网址图片(例如:http://imgur.com/6VZeFoS)
所以,我想将任何url-image-without-extension(或不以.jpg结尾)转换为格式http://i.imgur.com/<ID>.jpg
(例如:http://imgur.com/6VZeFoS - &gt;变为 - &gt; http://i.imgur.com/6VZeFoS.jpg)
我需要更改代码才能修复此问题?
这里是我用来解析网址图片的代码:
// getting image URLs
App.image_domains = {
'i.imgur.com': function (article) {
var url = article.data.url;
var split_url = url.split("imgur.com/");
if (split_url[1].indexOf(".") === -1) {
return url + ".jpg";
}
return url;
},
'imgur.com': function (article) {
var url = article.data.url;
// Ignore album images
if (url.indexOf("/a/") !== -1) {
return "";
}
// Remove unnecessary paths from image URLs
url = url.replace(/(www\.)?imgur.com/, "i.imgur.com");
url = url.replace(/\/r\/[^\/]+/, "");
url = url.replace("/new", "");
url = url.replace("/gallery", "");
// Make sure the URL doesnt already have an extension
var split_url = url.split("imgur.com/");
if (split_url[1].indexOf(".") === -1) {
url += ".jpg";
}
return url;
}
};
答案 0 :(得分:0)
另一个解决方案是 - http://jsfiddle.net/aimeeborda/aX6Te/where我过滤img的src
(.jpg|.png)$
不以jpg或png结尾并添加扩展名
var index = src.lastIndexOf('.') < 0 ? src.length : src.lastIndexOf('.');
$(this).attr('src', src.substring(0, index) + ".jpg");