简体   繁体   中英

How to get a CSS property of an HTML element from its class?

I am using javascript to get the src element of an img with this script:

document.addEventListener("click", function(evt) {
            var src = evt.target.src 
}

but some images don't have src attribute as it is set in the currentSrc property of its css class like this one:

<img alt="Chaussure de football enfant terrain sec Agility 900 FG bleu rouge" class="source-medium">

and the source-medium css class contains:

currentSrc: "https://contents.mediadecathlon.com/p1353468/k$6b31ed91fa5b73e99f89ff262c0985c7/s

My question is how to get the currentSrc property with javascript?

Did you try evt.target.currentSrc ?

If you only want the sources from the img elements with the source-medium class you can select those elements and get their source: document.querySelectorAll('.source-medium') and then loop over the result with a for loop or something.

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