我正在制作一个计算器而且我遇到了第一个JS障碍。我正在尝试在数字按钮上设置事件侦听器,以便在单击时运行函数,但是我得到错误,我无法在null上设置事件处理程序。但我看不出我错在哪里!?
我尝试来组织我的代码,就像我在线课程一样 - 分离出所有功能并试图让它易于阅读。
请参阅代码并提前致谢:
//TOTAL CONTROLLER
var totalController = (function () {
})();
//UI CONTROLLER
var UIController = (function () {
var DOMstrings = {
calcbuttons: '.calcButtons',
number: '.number',
operator: '.operator',
equals: '.equals',
total: '#total'
};
return {
getDOMstrings: function () {
return DOMstrings;
}
};
})();
//GLOBAL CONTROLLER
var controller = (function (totalCTRL, UICtrl) {
var setupEventListeners = function () {
var DOM = UICtrl.getDOMstrings();
var el = document.querySelector(".calcButtons");
el.addEventListener('click', function (e) {
//var table = e.target;
//if (table.classList)
console.log(e);
//ctrlAddNumber();
});
// click operator
//click C
//click AC
//click equals
};
var ctrlAddNumber = function () {
var selectedNumber;
//1. get number from html from click
selectedNumber = document.querySelector(DOM.number).innerHTML;
console.log(number);
//2. add to display
};
return {
init: function () {
console.log('Application has started.');
setupEventListeners();
}
};
})(totalController, UIController);
controller.init();
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<link type="text/css" rel="stylesheet" href="calc_style.css" />
<style>
@import url('https://fonts.googleapis.com/css?family=Lato');
</style>
<script type='text/javascript' src='calcjs.js'></script>
<title>JavaScript Calculator</title>
</head>
<body>
<div id="container">
<div id="calculator">
<div id=t otal></div>
<table class="calcButtons" cellspacing="0" cellpadding="0">
<tr>
<td class="wide">AC</td>
<td class="wide">←</td>
<td class="wide"></td>
<td class="operator"></td>
</tr>
<tr>
<td class="number">7</td>
<td class="number">8</td>
<td class="number">9</td>
<td class="operator">÷</td>
</tr>
<tr>
<td class="number">4</td>
<td class="number">5</td>
<td class="number">6</td>
<td class="operator">×</td>
</tr>
<tr>
<td class="number">1</td>
<td class="number">2</td>
<td class="number">3</td>
<td class="operator">-</td>
</tr>
<tr>
<td class="number">0</td>
<td>.</td>
<td id="equals" class="equals">=</td>
<td class="operator">+</td>
</tr>
</table>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
有几件事:
a
)元素在文档和按钮(button
)之间导航。tbody
。
const
calcButtons = document.querySelector('.calcButtons');
function onCalcButtonClicked(event) {
debugger;
const
clickedButton = event.target;
if (clickedButton.hasAttribute('data-number')) {
console.log('The user inputted a number: ', clickedButton.getAttribute('data-number'));
} else if (clickedButton.hasAttribute('data-action')) {
console.log('The user selected an action: ', clickedButton.getAttribute('data-action'));
}
}
calcButtons.addEventListener('click', onCalcButtonClicked);
&#13;
<div id="container">
<div id="calculator">
<div id="total"></div>
<table class="calcButtons" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><button type="button" data-action="" class="wide">AC</button></td>
<td><button type="button" data-action="" class="wide">←</button></td>
<td><button type="button" data-action="" class="wide"></button></td>
<td><button type="button" data-action="" class="operator"></button></td>
</tr>
<tr>
<td><button type="button" data-number="7" class="number">7</button></td>
<td><button type="button" data-number="8" class="number">8</button></td>
<td><button type="button" data-number="9" class="number">9</button></td>
<td><button type="button" data-action="plus" class="operator">÷</button></td>
</tr>
<tr>
<td><button type="button" data-number="4" class="number">4</button></td>
<td><button type="button" data-number="5" class="number">5</button></td>
<td><button type="button" data-number="6" class="number">6</button></td>
<td><button type="button" data-action="times" class="operator">×</button></td>
</tr>
<tr>
<td><button type="button" data-number="1" class="number">1</button></td>
<td><button type="button" data-number="2" class="number">2</button></td>
<td><button type="button" data-number="3" class="number">3</button></td>
<td><button type="button" data-action="minus" class="operator">-</button></td>
</tr>
<tr>
<td><button type="button" data-number="0" class="number">0</button></td>
<td><button type="button" data-number=".">.</button></td>
<td><button type="button" data-action="equals" id="equals" class="equals">=</button></td>
<td><button type="button" data-action="divide" class="operator">+</button></td>
</tr>
</tbody>
</table>
</div>
</div>
&#13;