因此,我正在构建一个 JavaScript 计算器,并且我有类数字,并且每当单击该类时,它都会迭代一个代码序列,正如您在下面的 javascript 中看到的那样,但它不会检索它单击的那个的 innerHTML 值由于某些原因。我需要继续使用 ID 还是可以完成这项工作?
JavaScript
$(document).ready(function(){
$('.numerical').on('click', function(){
var innerDisp = document.getElementById('display').innerHTML;
var num = document.getElementById(this).innerHTML;
document.getElementById('display').innerHTML = innerDisp + num;
});
});
HTML
<body>
<center>
<div id="calculator">
<div id="display"></div>
<button class="numerical" id="0">0</button>
<button class="numerical" id="1">1</button>
<button class="numerical" id="2">2</button>
<button class="function" id="add">+</button>
<button class="numerical" id="3">3</button>
<button class="numerical" id="4">4</button>
<button class="numerical" id="5">5</button>
<button class="function" id="subtract">-</button>
<button class="numerical" id="6">6</button>
<button class="numerical" id="7">7</button>
<button class="numerical" id="8">8</button>
<button class="function" id="multiply">x</button>
<button class="numerical" id="9">9</button>
<button class="function" id="c">C</button>
<button class="function" id="ce">CE</button>
<button class="function" id="divide">/</button>
</div>
</center>
<script src="calculator.js"></script>
</body>