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

时间:2013-02-19 21:48:35

标签: javascript html variables getelementbyid onmouseover

我正在撰写一个每页有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>

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

将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>