简体   繁体   English

未捕获的类型错误:无法在 HTMLButtonElement 处设置 null 的属性“textContent”。<anonymous> (index.js:17),为什么会这样?</anonymous>

[英]Uncaught TypeError: Cannot set property 'textContent' of null at HTMLButtonElement.<anonymous> (index.js:17), why is it happening?

I was doing a counter project and had this issue a couple of times but I couldn't find what the issue was so I copy and pasted the js from a counter project that free code camp did and that won't work.我正在做一个计数器项目并且遇到过几次这个问题,但我找不到问题所在,所以我从免费代码营所做的计数器项目中复制并粘贴了 js,但这不起作用。 What am I missing?我错过了什么? Also, any best practice tips and general advice is appreciated.此外,感谢任何最佳实践技巧和一般建议。

HTML HTML

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" type="text/css" href="index.css">
    <title>Counter</title>
  </head>
  <body>
    <div class="container">
      <div class="counterContainer">
        <h1>Counter</h1>
        <br>
        <div class="counter" id="counter">
          54
        </div>
      </div>
      <div class="buttonContainer">
        <button type="button" class="decreaseButton button" id="decreaseButton">
          Decrease
        </button>
        <button type="button" class="resetButton button" id="resetButton">
          Reset
        </button>
        <button type="button" class="increaseButton button" id="increaseButton">
          increase
        </button>
      </div>
    </div>

    <script src="index.js">
    </script>
  </body>
</html>

js js

// set inital value to zero
let count = 0;
// select value and buttons
const value = document.querySelector("counter");
const btns = document.querySelectorAll("button");

btns.forEach(function (btn) {
  btn.addEventListener("click", function (e) {
    const styles = e.currentTarget.classList;
    if (styles.contains("decreaseButton")) {
      count--;
    } else if (styles.contains("increaseButton")) {
      count++;
    } else {
      count = 0;
    }
    value.textContent = count;
  });
});

As noted in comments, mistake is here const value = document.querySelector("counter");如评论中所述,错误在这里const value = document.querySelector("counter"); This selector selects non-existent HTML element counter .此选择器选择不存在的 HTML 元素counter Should select by class or id, like const value = document.querySelector(".counter"); select 应该是 class 还是 id,比如const value = document.querySelector(".counter"); or const value = document.querySelector("#counter");const value = document.querySelector("#counter"); . . Both valid for this example, but for unique element id is preferred.两者都适用于本示例,但首选唯一元素 id。

Regarding general advice, this code is out of sync since start - shows 54, but the real value of counter is 0. Avoid that.关于一般建议,此代码自启动以来不同步 - 显示 54,但计数器的实际值为 0。避免这种情况。 You can set initial text in counter element from the actual value or vice versa - set counter value from the text value on page init.您可以根据实际值在计数器元素中设置初始文本,反之亦然 - 从页面 init 上的文本值设置计数器值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法在 HTMLButtonElement 处读取 null(读取“推送”)的属性。<anonymous> (main.js:15:23)</anonymous> - Uncaught TypeError: Cannot read properties of null (reading 'push') at HTMLButtonElement.<anonymous> (main.js:15:23) 未捕获的类型错误:firebase.firestore()…set 不是 HTMLButtonElement 的 function。<anonymous></anonymous> - Uncaught TypeError: firebase.firestore()…set is not a function at HTMLButtonElement.<anonymous> 未捕获的TypeError:$(…).modal在HTMLButtonElement中不是函数。 <anonymous> - Uncaught TypeError: $(…).modal is not a function at HTMLButtonElement.<anonymous> 收到以下错误:RGB.js:16 Uncaught TypeError: 无法读取 HTMLButtonElement 处未定义的属性“add”。<anonymous> (RGB.js:16) - Getting the following error: RGB.js:16 Uncaught TypeError: Cannot read property 'add' of undefined at HTMLButtonElement.<anonymous> (RGB.js:16) 未捕获的类型错误:无法将属性“textContent”设置为 null - Uncaught TypeError: Cannot set property 'textContent' of null 错误: Uncaught (in promise) TypeError: doc is not a function at HTMLButtonElement。<anonymous> - Error: Uncaught (in promise) TypeError: doc is not a function at HTMLButtonElement.<anonymous> 我得到:未捕获的类型错误:无法设置 null 的属性“textContent” - I am getting:Uncaught TypeError: Cannot set property 'textContent' of null (index):40 Uncaught TypeError: Cannot set property 'innerHTML' of null at getanswer ((index):40) at HTMLButtonElement.onclick ((index):204) - (index):40 Uncaught TypeError: Cannot set property 'innerHTML' of null at getanswer ((index):40) at HTMLButtonElement.onclick ((index):204) 未捕获的类型错误:无法读取 null 的属性“textContent” - Uncaught TypeError: Cannot read property 'textContent' of null index.js:28 未捕获类型错误:无法读取 null 的属性“addEventListener”(在 toggleActiveOnClick 和 Array.forEach 中) - index.js:28 Uncaught TypeError: Cannot read property 'addEventListener' of null ( at toggleActiveOnClick and at Array.forEach)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM