如何在帖子中获取所有图片而不是最后一张图片?我做错了,用以下代码?
<script src="http://imgtestblog.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script&callback=mytest"></script>
<script type='text/javascript'>
//<![CDATA[
function mytest(oneimage) {
img = [];
for (var PlusAllImages = "", i = 0; i < oneimage.feed.entry.length; i++) {
var posttitle = oneimage.feed.entry[i],
url;
if (i == oneimage.feed.entry.length) break;
for (var k = 0; k < posttitle.link.length; k++)
if ("alternate" == posttitle.link[k].rel) {
url = posttitle.link[k].href;
break
}
img[i] = RetrieveAndGetallimages("content" in posttitle ? posttitle.content.$t : "summary" in blogggerposttitle ? posttitle.summary.$t : "");
posttitle = posttitle.title.$t;
i < oneimage.feed.entry.length && (PlusAllImages += '<div class="my-img"><h3>' + posttitle + '</h3><a title="' + posttitle + '" href="' + url + '">' + getallimages + "</a></div>")
}
document.write(PlusAllImages)
}
function RetrieveAndGetallimages(oneimage) {
var PlusAllImages = document.createElement("div");
PlusAllImages.innerHTML = oneimage;
oneimage = PlusAllImages.getElementsByTagName("img");
if (1 == oneimage.length) getallimages = '<img width="300" height="200" src="' + oneimage[0].src + '" />';
else if (1 < oneimage.length)
for (PlusAllImages = 0; PlusAllImages < oneimage.length; PlusAllImages++) getallimages = '<img width="300" height="200" src="' + oneimage[PlusAllImages].src + '" />';
else getallimages = '<img width="300" height="200" src="https://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg" />';
return getallimages
};
//]]>
</script>
抱歉,我错过了输入密码,非常抱歉。
答案 0 :(得分:1)
您需要在for
函数中的RetrieveAndGetallimages
循环内进行更改 -
for (PlusAllImages = 0; PlusAllImages < oneimage.length; PlusAllImages++) getallimages += '<img width="300" height="200" src="' + oneimage[PlusAllImages].src + '" />';
正如您将注意到的那样,我们通过使用加法简写运算符来存储for
循环中getallimages
循环中迭代的所有图像,而不是正常赋值(请参阅{{ 3}})