简体   繁体   English

JS如何获取所有img src标签

[英]JS how to get all img src tags

I want to get the SRC from every <IMG> in twitter.我想从 Twitter 中的每个<IMG>获取 SRC。

let n = document.getElementsByTagName("img");

for(tip of n){
    console.log(tip.src);
}

and it works for other pages, but it doesn't for twitter.它适用于其他页面,但不适用于 twitter。 Finally I did this;最后我做到了;

let n = document.getElementsByTagName("img");

function example(){
    for(tip of n){
        if(tip.src == "https://abs-0.twimg.com/emoji/v2/svg/1f1ea-1f1f8.svg"){
            tip.src = "https://abs-0.twimg.com/emoji/v2/svg/1f3f3-fe0f-200d-1f308.svg";
        }
    }
}

setInterval(example, 100);

With setInterval it works, also, twitter loads stuff dynamically so with setInterval I can get all those new results.使用 setInterval 它也可以工作,而且 twitter 可以动态加载内容,因此使用 setInterval 我可以获得所有这些新结果。 (How I could do that without using setInterval?) (我怎么能在不使用 setInterval 的情况下做到这一点?)

Also, for some weird reason, the picture doesn't change.此外,由于某些奇怪的原因,图片没有改变。 It doesn't update.它不更新。

Update:更新:

function example(){
let n = document.getElementsByTagName("div");
    for(tip of n){
        console.log(tip.style['background-image']);

        if(tip.style['background-image'] == 'url("https://abs-0.twimg.com/emoji/v2/svg/1f1ea-1f1f8.svg")'){
            tip.style['background-image']= 'url("https://abs-0.twimg.com/emoji/v2/svg/1f3f3-fe0f-200d-1f308.svg")';
        }
    }
}

setInterval(example, 10000);

Now it works perfectly, but how could I get the new data without using setInterval eeeeeeevery time and only call to the function when it's necessary?现在它可以完美运行,但是如何在不使用 setInterval eeeeee 的情况下获取新数据,并且仅在必要时调用该函数?

You have to load image every time you want to iterate them, to get "fresh" img elements每次要迭代它们时都必须加载图像,以获得“新鲜”的 img 元素

Try this尝试这个

function example(){
    let n = document.getElementsByTagName("img");  // Moved here
    for(tip of n){
        if(tip.src == "https://abs-0.twimg.com/emoji/v2/svg/1f1ea-1f1f8.svg"){
            tip.src = "https://abs-0.twimg.com/emoji/v2/svg/1f3f3-fe0f-200d-1f308.svg";
        }
    }
}

setInterval(example, 100);

It is not always that the images are shown with img tag.图像并不总是用img标签显示。

If you see some cases on twitter, img tag's sibling is an element with img src in its styles.如果你在 twitter 上看到一些案例, img标签的兄弟是一个在其样式中带有img src的元素。 That element is showing the image.该元素正在显示图像。 Obviously, this is not the case with every single image on twitter.显然,推特上的每一张图片都不是这种情况。

But for such a case, you change it's background by但是对于这种情况,您可以通过以下方式更改它的背景

let n = document.getElementsByTagName("img");

for(tip of n){
    r.previousSibling.style.backgroundImage='YourImage.png'
}

Also, you have to use some kind of interval to get results that are loaded later.此外,您必须使用某种间隔来获取稍后加载的结果。 Or you could detect DOM changes using Mutation Observer and only fire your function on DOM change.或者您可以使用Mutation Observer检测 DOM 更改,并且仅在 DOM 更改时触发您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM