【发布时间】:2022-11-01 22:21:27
【问题描述】:
有没有办法缩短 JavaScript 中 if 语句中的代码,我正在考虑 for 循环,但我不知道如何以及是否可能。我已经把我认为的相关代码
var operators = document.querySelectorAll(".operators button");
var string = screenInput.innerHTML
var lastCharacter = string[string.length - 1]
console.log(lastCharacter)
if (lastCharacter === "+") {
document.getElementById("subtract-operator").disabled = true
document.getElementById("multiply-operator").disabled = true
document.getElementById("divide-operator").disabled = true
}
<div class="operators">
<button id="add-operator">+</button>
<button id="subtract-operator">-</button>
<button id="multiply-operator">*</button>
<button id="divide-operator">/</button>
</div>
【问题讨论】:
-
是只有这个
if语句,还是每个运算符都有语句(禁用其他运算符按钮)?
标签: javascript button