[英]How to select image elements with its srcset attribute value using javascript?
例如,
<img class="page-hero__image" style="" srcset="https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1400x0-c-default.jpg 1600w,
https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1200x0-c-default.jpg 1200w,
https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1024x0-c-default.jpg 1024w,
https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-800x0-c-default.jpg 800w,
https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-400x0-c-default.jpg 400w" sizes="(min-width: 1200px) 1200px, 100vw" src="https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1000x0-c-default.jpg" alt="">
我需要使用document.querySelectorAll('img[srcset='value']')
通過 srcset 屬性及其值選擇上述 img 元素。 我怎樣才能做到這一點?
您需要先找到圖像元素並通過 srcset 過濾
var images = document.getElementsByTagName('img');
for(var i = 0; i < images.length; i++) {
if(images[i].src == srcset) //check with your srcset
console.log("found image element"); //image[i] has found element
}
或者你也可以用這種方式
document.querySelectorAll('img[srcset="value"]');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.