简体   繁体   English

在带有 class 的链接下抓取 img 标签

[英]Grab img tag under a link with a class

So I have a img that I need to change and I can't access the html right now so need to do it with just css所以我有一个需要更改的 img,我现在无法访问 html,所以只需要使用 css 即可

The structure is结构是

<a href="afd.com" class="imgfile">
  <img src = "adsfasd">
</a>

I need to change the src of the img, but I'm not sure how to grab it.我需要更改img的src,但我不确定如何抓取它。 I tried 'grab element by selector' in chrome but that's not working exactly.我在 chrome 中尝试了“通过选择器抓取元素”,但这并不完全有效。

Once I grab it I'll just do something like一旦我抓住它,我就会做类似的事情

var image = document.querySelector(".example");
image.src = "image1.jpg"

Note, NO jquery注意,没有 jquery

You can select just like CSS in querySelector您可以 select 就像querySelector中的 CSS

 const image = document.querySelector("a.imgfile img"); image.src = "https://source.unsplash.com/random/200x150";
 <a href="afd.com" class="imgfile"> <img src="adsfasd" /> </a>

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

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