如何修复Uncaught TypeError: Cannot read properties of null (reading ‘addEventListener’)

huangapple go评论95阅读模式
英文:

How to fix Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

问题

我有一个用于计算器应用的 HTML 脚本,带有添加的按钮进行点击操作。我想使用 eventListener 获取用户点击的按钮,但我无法这样做。我得到了以下错误:

  1. Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
  2. init (script.js:86:43)
  3. at script.js:93:1

在 script.js 中我使用了以下代码:

  1. function init() {
  2. document.querySelector('calc-buttons').
  3. addEventListener('click', function (event) {
  4. console.log(event, "event in init")
  5. buttonClick(event.target.innerText);
  6. })
  7. }
  8. console.log('here')
  9. init();

用于捕获按钮点击,但在 eventListeners 中抛出错误。有人可以帮助修复这个问题吗?

英文:

Im having this html script for a calculator App, with added buttons to click.
I want to use eventLister to get the button clicked by the user but I'm not able to do so.
Im getting
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
init (script.js:86:43)
at script.js:93:1
in console.

  1. <body>
  2. <div class="wrapper">
  3. <section class="screen">
  4. 0
  5. </section>
  6. <section class="calc-buttons">
  7. <div class="calc-button-row">
  8. <button class = "calc-button double">C</button>
  9. <button class="calc-button">←</button>
  10. <button class="calc-button">÷</button>
  11. </div>
  12. <div class="calc-button-row">
  13. <button class="calc-button">7</button>
  14. <button class="calc-button">8</button>
  15. <button class="calc-button">9</button>
  16. <button class="calc-button">×</button>
  17. </div>
  18. <div class="calc-button-row">
  19. <button class="calc-button">4</button>
  20. <button class="calc-button">5</button>
  21. <button class="calc-button">6</button>
  22. <button class="calc-button">−</button>
  23. </div>
  24. <div class="calc-button-row">
  25. <button class="calc-button">1</button>
  26. <button class="calc-button">2</button>
  27. <button class="calc-button">3</button>
  28. <button class="calc-button">+</button>
  29. </div>
  30. <div class="calc-button-row">
  31. <button class="calc-button triple">0</button>
  32. <button class="calc-button">=</button>
  33. </div>
  34. </section>
  35. </div>
  36. <script src="script.js"></script>
  37. </body>

In script.js I'm using,

  1. function init() {
  2. document.querySelector('calc-buttons').
  3. addEventListener('click', function (event) {
  4. console.log(event, "event in init")
  5. buttonClick(event.target.innerText);
  6. })
  7. }
  8. console.log('here')
  9. init();

to capture the button clicked but its throwing error in eventListners.
Can someone help fix the issue?

答案1

得分: 3

你需要告诉 querySelector,输入的字符串是类名('.calc-buttons')还是ID('#calc-buttons')。

当你使用 document.querySelector('.calc-buttons') 时,你实际上是选择了section标签。

如果你想选择所有按钮,你需要使用:

  1. document.querySelectorAll('.calc-button')

然后你需要遍历所有按钮,为它们添加事件监听器。例如像这样:

  1. const buttons = document.querySelectorAll('.calc-button')
  2. for (let i = 0; i < buttons.length; i++) {
  3. buttons[i].addEventListener('click', function (event) {
  4. buttonClick(event.target.innerText);
  5. })
  6. }

因此,整个代码看起来应该是这样的:

  1. function init() {
  2. const buttons = document.querySelectorAll('.calc-button')
  3. for (let i = 0; i < buttons.length; i++) {
  4. buttons[i].addEventListener('click', function (event) {
  5. console.log(event, "event in init")
  6. buttonClick(event.target.innerText);
  7. })
  8. }
  9. }
  10. console.log('here')
  11. init();

注意:以上代码示例已被翻译为中文。

英文:

You need to let querySelector know, if the entered string is a class name ('.calc-buttons') or an id ('#calc-buttons').

Also, you are selecting the section tag when you use document.querySelector(&#39;.calc-buttons&#39;)

If you want to select all buttons, you need to use:

  1. document.querySelectorAll(&#39;.calc-button&#39;)

And then you need to loop through the buttons to add event listeners to all buttons. For example like this:

  1. const buttons = document.querySelectorAll(&#39;.calc-button&#39;)
  2. for (let i = 0; i &lt; buttons.length; i++) {
  3. buttons[i].addEventListener(&#39;click&#39;, function (event) {
  4. buttonClick(event.target.innerText);
  5. })
  6. }

So, the whole thing would look like this:

  1. function init() {
  2. const buttons = document.querySelectorAll(&#39;.calc-button&#39;)
  3. for (let i = 0; i &lt; buttons.length; i++) {
  4. buttons[i].addEventListener(&#39;click&#39;, function (event) {
  5. console.log(event, &quot;event in init&quot;)
  6. buttonClick(event.target.innerText);
  7. })
  8. }
  9. }
  10. console.log(&#39;here&#39;)
  11. init();

答案2

得分: 0

在你的代码中,你忘记在querySelector中类名前加点:正确的代码是:

  1. function init() {
  2. document.querySelector('.calc-buttons')
  3. .addEventListener('click', function (event) {
  4. console.log(event, "init中的事件")
  5. buttonClick(event.target.innerText);
  6. });
  7. }
  8. console.log('这里');
  9. init();
英文:

In your code you forgot the dot before the class name in the querySelector :
The correct code is :

  1. function init() {
  2. document.querySelector(&#39;.calc-buttons&#39;)
  3. .addEventListener(&#39;click&#39;, function (event) {
  4. console.log(event, &quot;event in init&quot;)
  5. buttonClick(event.target.innerText);
  6. });
  7. }
  8. console.log(&#39;here&#39;);
  9. init();

huangapple
  • 本文由 发表于 2023年5月10日 14:52:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定