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

前端 未结 5 1372
感动是毒
感动是毒 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:45

    To print on current page (replace content):

    document.write(`
    ${JSON.stringify(Array.prototype.map.call(document.getElementsByTagName("img"), img => img.src), null, 2)}
    `);

    To save in array:

    const imgs = Array.prototype.map.call(document.getElementsByTagName("img"), img => img.src);
    

    To just console dir/log directly:

    console.dir(Array.prototype.map.call(document.getElementsByTagName("img"), img => img.src));
    console.log(Array.prototype.map.call(document.getElementsByTagName("img"), img => img.src));
    

提交回复
热议问题