开发者

Breaking up an array of document.getElementsByTagName("img");

I am using the following to find all of the images on a given page:

function img_find() {
var imgs = document.getElementsByTagName("img");
var imgSrcs = [];
for (var i = 0; i < imgs.length; i++) {
    ;
    imgSrcs.push(imgs[i].src);
}
return imgSrcs;
}

I think define a new variable img_find = img_find(); and then I write the variable document.write(img_find);

This is my output string "http://www.domain.com/image.png"

I want to break this up so eventually I could write

document.write("<img src='"+img_find+"'/>");

Which would display the images.

However as of now all it outputs is

src='http://www.domain.com/image.png,http://www.domain.com/image.png'

which obviously won't display an image.

Does anybody know how I can re-write this so I can use document.write("<img src='"+img_find+"'/>");开发者_JS百科 and have it display all the images on the current page?

Thanks!


It is time for the magnificent for loop! Saving desperate coders everywhere!

var img_ = img_find();

for(var i=0; i<img_.length; i++){
    document.write("<img src='"+img_[i]+"'/>");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜