How to get all the image sources on a particular Page using Javascript

前端 未结 5 1362
感动是毒
感动是毒 2021-02-08 03:26

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].sr         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-08 03:58

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

提交回复
热议问题