2015-10-05 187 views
-2

我是JavaScript新手,需要一些幫助。我正在一個網站的橙色, 網站我有兩個按鈕(一)。 ,我有兩個div可以通過點擊一個按鈕來顯示。 我有三個紅色,藍色和橙色,通過點擊它們 紅色,標題,按鈕和div更改爲藍色,紅色或橙色陰影。 當我點擊一個按鈕(一)他的顏色變成白色。 當我推動另一個按鈕時,他變成了白色,第一次變成橙色(這很好)。 但如果我將頁面轉爲藍色陰影,請單擊第一個按鈕,然後在第二個按鈕上第一個按鈕變回橙色而不是藍色。 我該怎麼做纔對?更改變量值onclick

我試過在函數中做一個if語句和一個變量。 當我使它在我寫的var shades =「blue」(和紅色和橙色相同)的函數中變成藍色時。 然後在一個按鈕功能,我已經寫了顏色背景爲白色,然後 將ather按鈕變爲if語句(如果陰影=「藍色」將背景變成藍色) 然後另一個語句(如果陰影=「紅色」將背景變成紅色)和橙色相同。 但它不起作用,因爲變量值只在函數中改變,然後停止。 如何通過單擊按鈕來更改變量值?

謝謝。

爲例:

<header> 
    <ul id="headerul"> 
     <li><a onclick="page1()" href="#" id="a" >page1</a></li> 
     <li><a onclick="page2()" href="#" id="a1">page2</a></li> 

     <div id="blue" onclick="colorB()"></div> 
     <div id="red" onclick="colorR()"></div> 
     <div id="orange" onclick="colorO()></div> 
    </ul> 
</header> 

<script> 
     function colorB(){ 
     document.getElementById('body').style.cssText = 
     'background-color:a7a7ff;' 
     document.getElementById('headerul').style.cssText = 
     'background-color:7777ff;' 
     document.getElementById('a').style.cssText = 
     'background-color:#a7a7ff;' 
     document.getElementById('a1').style.cssText = 
     'background-color:#a7a7ff;' 
     var body = 'blue' 
    } 
    function colorR(){ 
     document.getElementById('body').style.cssText = 
     'background-color:ff5252;' 
     document.getElementById('headerul').style.cssText = 
     'background-color:ff2222;' 
     document.getElementById('a').style.cssText = 
     'background-color:#ff5252;' 
     document.getElementById('a1').style.cssText = 
     'background-color:#ff5252;' 
     var body = 'red' 
    } 
    function colorO(){ 
     document.getElementById('body').style.cssText = 
     'background-color:#ffc5a0;' 
     document.getElementById('headerul').style.cssText = 
     'background-color:#ff8027;' 
     document.getElementById('a').style.cssText = 
     'background-color:#ffc5a0;' 
     document.getElementById('a1').style.cssText = 
     'background-color:#ffc5a0;' 
     var body = 'orange' 
    } 
    function page1(){ 
     document.getElementById('a1').style.cssText = 
     'background-color:white;' 
       if (body=="blue") { 
       document.getElementById('a').style.cssText = 
      'background-color:#a7a7ff;' 
     }; 
       if (body=="orange") { 
       document.getElementById('a').style.cssText = 
       'background-color:#ffc5a0;' 
     }; 
    } 
    function page2(){ 
     document.getElementById('a').style.cssText = 
     'background-color:white;' 
       if (body=="blue") { 
       document.getElementById('a1').style.cssText = 
      'background-color:#a7a7ff;' 
     }; 
       if (body=="orange") { 
       document.getElementById('a1').style.cssText = 
       'background-color:#ffc5a0;' 
     }; 
    } 
</script> 
+4

我將是一個很大的幫助如果你可以顯示你當前的一些代碼。 – Berendschot

+0

嗨歡迎來到StackOverflow - 這是一個好主意,閱讀關於[如何問一個好問題]的幫助主題(http://stackoverflow.com/help/how-to-ask)。目前這個問題是不是可以得到一個很好的答案,這將有助於你以任何有意義的方式。 – Jamiec

回答

1

這幾乎是不可能的,沒有看到你的代碼,但在猜測這可能會幫助你以某種方式:

https://jsfiddle.net/1xv6qjpx/

I would add this as a comment but I don't yet have the ability to do so.