如何从Json检索所有图像?

时间:2017-03-24 20:04:38

标签: javascript json blogger

如何在帖子中获取所有图片而不是最后一张图片?我做错了,用以下代码?

<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>

抱歉,我错过了输入密码,非常抱歉。

1 个答案:

答案 0 :(得分:1)

您需要在for函数中的RetrieveAndGetallimages循环内进行更改 -

for (PlusAllImages = 0; PlusAllImages < oneimage.length; PlusAllImages++) getallimages += '<img width="300" height="200" src="' + oneimage[PlusAllImages].src + '" />';

正如您将注意到的那样,我们通过使用加法简写运算符来存储for循环中getallimages循环中迭代的所有图像,而不是正常赋值(请参阅{{ 3}})