我有一个非常具体的用例,我需要获取一个dom元素,它是一个 anchor 标记,但它没有类或id,它唯一的属性是“rel”,“ios-src”它有一个 <img />
也没有 id 或类(class)的 child 。我无法修改代码。
如何在纯 JavaScript 中查询元素?
这是 anchor 标记的外观:
<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>
关于javascript - 不使用 id 或 class 获取 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61519582/