2013-10-11 53 views
0

我想這HTML代碼:如何解決函數內部調用函數的元素?

<button name="darkBlue" onclick="setThemeColor(this.name)">Blue</button> 
<button name="black" onclick="setThemeColor(this.name)">Black</button> 

和腳本:

function setThemeColor(buttonName) { 
    localStorage.themeColor = buttonName; 
    document.getElementsByTagName('html')[0].className = buttonName 
    var themeButtons = document.querySelectorAll(".theme"); 
    for (var button in themeButtons) { 
     themeButtons[button].disabled = false; 
    } 
    // this.disabled = false; 
    // element.setAttribute("disabled", "disabled"); 
} 

我有一個問題就在這裏設置調用該功能按鈕的禁用狀態。有人能告訴我我該怎麼做。我嘗試了兩件事,但都沒有奏效。

+0

@DavidThomas你在哪裏? – VisioN

+0

'function setThemeColor(sender,buttonName)''>'setThemeColor(this,this.name)'。那麼你可以通過發件人,並在函數中獲得其名稱再次將其減少到一個參數 – DarkBee

+0

@RGraham已經做 – DarkBee

回答

5

傳遞到您的按鈕的引用,而不是僅僅名稱:

HTML

<button name="darkBlue" onclick="setThemeColor(this)">Blue</button> 
<button name="black" onclick="setThemeColor(this)">Black</button> 

JS

function setThemeColor(button) { 
    localStorage.themeColor = button.name; 
    document.getElementsByTagName('html')[0].className = button.name; 
    var themeButtons = document.querySelectorAll(".theme"); 
    for (var button in themeButtons) { 
     themeButtons[button].setAttribute("disabled", "disabled"); 
    } 
    button.setAttribute("disabled", "disabled"); 
} 
+0

你可以確認,如果我只是其中一種方法來設置禁用或我需要兩條線嗎? – 2013-10-11 11:15:57

+0

是的,只需使用'setAttribute'。不需要'disabled = false'行。更新的答案反映了這一點 – CodingIntrigue

-2
document.getElementById("buttonid1").disabled=false;