我正在制作小费计算器,但出现错误
function CalcTip(){
var bill = document.querySelector('#bill').value;
var people = document.querySelector('#people').value;
var button = document.querySelector('button');
var total = bill.value/people.value;
document.getElementById("#last").innerHTML = "$" + total;
total = Math.round(total * 100) / 100;
}
button.addEventListener('click',function(){} calcTip(););
错误在您的addEventListener上,应该是
button.addEventListener('click', function(){ calcTip(); });
您的代码中有几个错误。
这里是一个功能示例:
var button = document.getElementById("calculate");
button.addEventListener('click', CalcTip);
function CalcTip(){
var bill = document.getElementById('bill');
var people = document.getElementById('people');
var total = parseInt(bill.value) / parseInt(people.value);
total = Math.round(total * 100) / 100;
document.getElementById("last").innerHTML = "$" + total;
}
<button type="button" id="calculate">Click Me!</button><br>
<input type="text" id="bill"><br>
<input type="text" id="people"><br>
<div id="last"></div>