2013-04-06 22 views
1

我正在嘗試創建一個頁面,它只是一個頁面,其中不同的div在未選中時隱藏。Javascript菜單高亮切換器

目前我遇到菜單按鈕出現問題,我希望選定的頁面高亮顯示,然後選擇另一個選項卡時,另一個選項卡將突出顯示。

說 主頁,關於我們,聯繫我們

所以,如果在家裏DIV,主頁選項卡被突出顯示,但如果它的關於我們的div中,關於我們高亮顯示。我想使用純CSS,但搜索後沒有任何結果,所以我必須堅持使用JavaScript。

這是我的代碼

function Switcher(a,b,c,d,e){ 
document.getElementById('button1').style.background=a; 
document.getElementById('button2').style.background=b; 
document.getElementById('button3').style.background=c; 
document.getElementById('button4').style.background=d; 
document.getElementById('button5').style.background=e; 
} 

與一個onClick功能

onClick="Switcher(#c5e043,#241009,#241009,#241009,#241009)" 

,但它似乎沒有工作,任何幫助嗎?或其他更簡單的建議:)?

回答

1

,而不是使用背景backgroundColor

function Switcher(a,b,c,d,e){ 
    document.getElementById('button1').style.backgroundColor = a; 
    document.getElementById('button2').style.backgroundColor = b; 
    document.getElementById('button3').style.backgroundColor = c; 
    document.getElementById('button4').style.backgroundColor = d; 
    document.getElementById('button5').style.backgroundColor = e; 
} 

而且傳遞的參數字符串:

onClick="Switcher('#c5e043', '#241009', '#241009', '#241009', '#241009')" 
+0

噢,總是缺少次要的細節:d多謝!還是一個新的編碼器:) – Crays 2013-04-06 15:42:36

0

或者其他簡單的建議:)?

腳本:

function Switcher(Colors) 
    { 
    var Count=1; 

Colors.split(",").forEach(function(xColor) { document.getElementById('button' + Count).style.background = xColor; 
Count +=1; }); 

} 

的onclick:

onClick="Switcher("#c5e043,#241009,#241009,#241009,#241009")"