gpt4 book ai didi

javascript - 如何将 HTML id 存储为 js 变量?

转载 作者:行者123 更新时间:2023-11-30 18:09:26 24 4
gpt4 key购买 nike

我正在编写一个每页将有 200 多个链接的网页。每个链接都有一个唯一的 ID,与另一个框架中的 ID 相匹配。我想运行一个 onmouseover 函数,它将跨框架更改两个链接的文本颜色。这是我到目前为止所得到的。

<html><head><title>Test</title>
<script>
function hey()
{var id=//HELP PLEASE; document.getElementById(id).style.color = "red";}
function bye()
{var id=//HELP PLEASE; document.getElementById(id).style.color = "black";}
</script>
</head>
<body>
<a id="1" class="word" onmouseover="hey()" onmouseout="bye()">hello</a>
<a id="2" class="word" onmouseover="hey()" onmouseout="bye()">world</a>....
</body></html>

有什么想法吗?

最佳答案

将 id 传递给函数:

<html><head><title>Test</title>
<script>
function hey(id)
{document.getElementById(id).style.color = "red";}
function bye(id)
{document.getElementById(id).style.color = "black";}
</script>
</head>
<body>
<a id="1" class="word" onmouseover="hey(this.id)" onmouseout="bye(this.id)">hello</a>
<a id="2" class="word" onmouseover="hey(this.id)" onmouseout="bye(this.id)">world</a>....
</body></html>

关于javascript - 如何将 HTML id 存储为 js 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14968108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com