繁体   English   中英

如何获取<a>标签</a>内img alt的文本

[英]How to get the text of img alt inside <a> tag

我有一个包含以下 html 部分的网址

 <div class="shop cf">
 <a class="shop-logo js-shop-logo" href="/m/3870/GMobile">
 <noscript>

<img alt="GMobile" class="js-lazy" data-src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" /> 
 </noscript>

<img alt="GMobile" class="js-lazy" data-src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" src="//c.scdn.gr/assets/transparent-325472601571f31e1bf00674c368d335.gif" />

</a>
</div>

我想在 div 类商店 cf 中获得第一个 img alt,我这样做了

  Set seller = Doc.querySelectorAll("img")      
  wks.Cells(i, "D").Value = seller.getAttribute("alt").Content(0)

我什么也没得到,我忘了包括什么???

我可以从

<noscript>

标签?

我也尝试了以下

  Set seller = Doc.getElementsByClassName("js-lazy")

  wks.Cells(i, "D").Value = seller.getAttribute("alt")

使用带有属性选择器的元素

CSS:

img[alt]

VBA:

ie.document.querySelector("img[alt]")

您可能需要添加

ie.document.querySelector("img[alt]").getAttribute("alt")

包括类使用

ie.document.querySelector("img.js-lazy[alt]")

如果有多个元素,则使用 querySelectorAll 并索引到返回的 nodeList 中,例如

Set list = ie.document.querySelectorAll("img.js-lazy[alt]")
list.item(0).getAttribute('alt')
list.item(1).getAttribute('alt')

你尝试过这种方式吗?

 let lazy1 = document.querySelectorAll(".js-lazy")[0]
 let lazyalt = lazy1.getAttribute("alt");
 let shop = document.querySelector('.shop');
 shop.classList.add(lazyalt);

    console.log(lazyalt)

暂无
暂无

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

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