简体   繁体   中英

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

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. I tried 'grab element by selector' in chrome but that's not working exactly.

Once I grab it I'll just do something like

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

Note, NO jquery

You can select just like CSS in querySelector

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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