<button id="a" onclick="if (getElementById('b').disabled==false) {getElementById('b').disabled=true; getElementById('c').disabled=true;} else {getElementById('b').disabled=false; getElementById('c').disabled=false;}">a</button>
<button id="b">b</button>
<button id="c">c</button>
或者你可以使它更清晰的代碼功能:
function updateBD(){
if (getElementById('b').disabled==false){
getElementById('b').disabled=true;
getElementById('c').disabled=true;
} else {
getElementById('b').disabled=false;
getElementById('c').disabled=false;
}
}
<button id="a" onclick="updateBC()">a</button>
<button id="b">b</button>
<button id="c">c</button>
該功能的作用是檢查b按鈕是否啓用(getElementById('b').disabled==false
),如果是則禁用按鈕b和c(getElementById('b').disabled=true; getElementById('c').disabled=true;
)。此外,由於它們被禁用(getElementById('b').disabled=false; getElementById('c').disabled=false;
),它使它們恢復原狀。
請添加上的,爲什麼後趴下投票 – MJK
使用功能的註釋部分和事件偵聽器的原因,通常優於聯事件屬性。 – evolutionxbox