我有以下HTML:如果我只知道該元素的名稱,如何禁用我的頁面上的按鈕?
<button name="darkBlue" onclick="setThemeColor(this.name)">Blue</button>
<button name="black" onclick="setThemeColor(this.name)">Black</button>
與此腳本:
if (localStorage.buttonColor) {
var themeButtons = document.querySelectorAll(".theme");
for (var button in themeButtons) {
themeButtons[buttons].removeAttribute("disabled");
}
// I need here to disable the button with the name that matches localstorage name
}
我已經有到位的方式從所有按鈕刪除禁用。但我怎麼才能禁用與localStorage.buttonColor同名的按鈕,而不使用jQuery?
我也可以在for (var button in themeButtons)
循環中做所有這些嗎?如果我能做到這一點,它可能會更加清潔解決方案。
'document.getElementsByName'? – putvande