【发布时间】:2022-01-09 14:44:27
【问题描述】:
我想用 JavaScript 更改(或控制...)我的 <a> href
<a class="google" href="#">LINK ONE</a>
<a class="google" href="#">LINK TWO</a>
还有一个 JavaScript :
document.querySelectorAll("a.google").href = "https://google.com"
如果我使用querySelector(".google") 它会更改链接,但只是主题之一。如果我使用querySelectorAll(".google") 或("a.google") 它不会改变任何东西......
谁能帮帮我?
【问题讨论】:
-
实际上
document.querySelectorAl返回一个元素数组,你应该使用像for这样的迭代来访问每个元素然后更改href -
阅读文档:
querySelector、querySelectorAll。请尝试使用浏览器的debugging capabilities。使用browser console (dev tools)(点击F12)并查看document.querySelector("a.google")和document.querySelectorAll("a.google")是什么。 “Can someone help me?” is not an actual question.
标签: javascript html href anchor