如何使用javascript连续追加12个图像

时间:2014-08-14 08:42:53

标签: javascript html image

function stripTags(s, n) {
    return s.replace(/<\/?(?!br\s*\/?)[a-z][a-z0-9]*[^<>]*>/ig, "").split(/\s+/).slice(0, n - 1).join(' ');
}

function rm(a, b, c) {
    var p = document.getElementById(a);
    var summ = summary;
    var summ = summary;
    imgtag = "";
    var i = 0;
    img = p.getElementsByTagName("img");
    imgsrc = img[i].src;
    var q = document.getElementById(b);
    var r = document.getElementById(c);

    for (var i = 0; i < img.length; i++) {
        if (img.length >=12) imgtag = '<a class="featured_gallery" href="' + imgsrc + '"><img src="' + imgsrc + '" /></a>';
        else if (img.length >=8) imgtag = '<a class="featured_gallery" href="' + img[0].src + '"><img src="' + img[0].src + '" /></a><a class="featured_gallery" href="' + img[1].src + '"><img src="' + img[1].src + '" /></a><a class="featured_gallery" href="' + img[2].src + '"><img src="' + img[2].src + '" /></a><a class="featured_gallery" href="' + img[3].src + '"><img src="' + img[3].src + '" /></a><a class="featured_gallery" href="' + img[4].src + '"><img src="' + img[4].src + '" /></a><a class="featured_gallery" href="' + img[5].src + '"><img src="' + img[5].src + '" /></a><a class="featured_gallery" href="' + img[6].src + '"><img src="' + img[6].src + '" /></a><a class="featured_gallery" href="' + img[7].src + '"><img src="' + img[7].src + '" /></a>';
        else if (img.length >= 1) imgtag = '<a href="' + y + '"><img src="' + img[0].src + '" style="max-width:100%;" alt="' + x + '" /></a>';
        else imgtag = '<div class="no-img"/>';
    }
}

这是我用来以图片形式从帖子内部获取图片的代码,其中包含至少12张图片。

但它只显示结果中的一个图像

<a href="http://3.bp.blogspot.com/-J3qrDYeQH4U/U-rj0I955_I/AAAAAAAAG7E/ZLvcdGFVVRc/s1600/12.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-J3qrDYeQH4U/U-rj0I955_I/AAAAAAAAG7E/ZLvcdGFVVRc/s1600/12.jpg" height="213" width="320"></a>

但我想连续拍摄12张图像,并希望使用“i array”简化“if”和“else if”部分代码

else if (img.length >=8) imgtag = '<a class="featured_gallery" href="' + img[0].src + '"><img src="' + img[0].src + '" /></a><a class="featured_gallery" href="' + img[1].src + '"><img src="' + img[1].src + '" /></a><a class="featured_gallery" href="' + img[2].src + '"><img src="' + img[2].src + '" /></a><a class="featured_gallery" href="' + img[3].src + '"><img src="' + img[3].src + '" /></a><a class="featured_gallery" href="' + img[4].src + '"><img src="' + img[4].src + '" /></a><a class="featured_gallery" href="' + img[5].src + '"><img src="' + img[5].src + '" /></a><a class="featured_gallery" href="' + img[6].src + '"><img src="' + img[6].src + '" /></a><a class="featured_gallery" href="' + img[7].src + '"><img src="' + img[7].src + '" /></a>';

任何最好的javascript都可以帮助我解决这个问题。

我只想在javascript中严格要求

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些逻辑错误,如果您使用迭代语句,请更改

imgtag+='<a href="' + y + '"><img src="' + img[i].src + '" style="max-width:100%;" alt="' + x + '" /></a>'