如何使用Javascript获取特定页面上的所有图像源 [英] How to get all the image sources on a particular Page using Javascript

查看:117
本文介绍了如何使用Javascript获取特定页面上的所有图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个简单的脚本在页面上查找图像并获取其来源。

I am using a simple script to find an image on a page and get its source.

function img_find() {
    var img_find2 = document.getElementsByTagName("img")[0].src;
    return img_find;
}

然而,当我在我的页面上写这个函数时,它只找到第一个图像,然后停止。在当前页面上打印所有图像src的最佳方法是什么?
谢谢!

However when I go to write this function on my page it only finds the first image and then stops. What is the best way to have it print all of the image src's on the current page? Thanks!

推荐答案

你确实告诉代码这样做了。不要那样做。只需告诉它循环遍历所有图像并将每个图像的src推入数组并返回所有srcs的数组。

You indeed told the code to do so. Don't do that. Just tell it to loop over all images and push the src of each in an array and return the array with all srcs instead.

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

这篇关于如何使用Javascript获取特定页面上的所有图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆