2013-02-19 24 views
1

我在寫一個每頁有200多個鏈接的網頁。每個鏈接都有一個唯一的ID,與另一個框架中的ID匹配。我想運行一個onmouseover函數,它將改變跨兩個框架的文字顏色。這是迄今爲止我所擁有的。如何將HTML標識存儲爲js變量?

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

有什麼想法?

+0

BTW。我的問題是,我不知道如何獲得存儲爲js變量的鏈接ID。 – parap 2013-02-19 21:51:53

+0

你剛纔在你的代碼中做過... document.getElementById(id).style.color =「red」;應該可以工作 – Saturnix 2013-02-19 21:52:25

+0

您可能已經知道這一點,但是您的評論阻止了你的'hey'和'bye'功能的末端括號 – EpicPineapple 2013-02-19 21:52:46

回答

1

傳遞標識加入功能:

<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> 
+0

謝謝。那樣做了。 – parap 2013-02-20 03:23:51

相關問題