如何使用 <script> 更改 href?

How to change href using <script>?

我试图在脚本中 400 毫秒后更改 id 的 href,但是当我转到该页面时,400 毫秒后 link 仍然相同。

<script>
    setTimeout(function(){document.getElementById(3).href=
    '/cgi-bin/mancalaV9eam.cgi/retro/2/3/1/4/4/4/0/5/5/1/5/4/4/4/4/4/0';}, 400);
</script>

元素的 id 是一个字符串,因此您需要将对 getElementById 的调用中的 id 括在引号中。

 setTimeout(function(){document.getElementById("3").href=
    '/cgi-bin/mancalaV9eam.cgi/retro/2/3/1/4/4/4/0/5/5/1/5/4/4/4/4/4/0';}, 400);
<a href="http://google.com" id="3">Does this still go to google?</a>

正如评论中指出的那样,id 通常不应以数字开头,但它确实有效。

Document.getElamentById 需要 case-sensitive string representing the unique ID of the element being sought.

因此您需要向其传递一个字符串,但不清楚脚本中的 3 到底指的是什么。