2013-10-11 40 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;