在不使用id或类的情况下获取DOM元素



我有一个非常具体的用例,我需要得到一个dom元素,它是一个锚标记,但它没有类或id,它唯一的属性是"rel"、"ios src",它有一个<img />子级,它也没有id或类。我无法修改代码。

如何用纯javascript查询元素?

锚标签的外观如下:

<a rel="ar" href="https://example.com/cool-model.usdz#applePayButtonType=pay....etc">  <img src="poster.jpg"></a>

您可以使用querySelector

console.log(document.querySelector("a[rel='ar']"))
<a rel="ar" href="https://example.com/cool-model.usdz#applePayButtonType=pay....etc"> <img src="poster.jpg"></a>

最新更新