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

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

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

问题

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

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

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

function init() {
    document.querySelector('calc-buttons').
        addEventListener('click', function (event) {
            console.log(event, "event in init")
            buttonClick(event.target.innerText);
        })
}
console.log('here')
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.

<body>
    
     <div class="wrapper">
        <section class="screen">
            0
        </section>
        <section class="calc-buttons">
            <div class="calc-button-row">
                <button class = "calc-button double">C</button>
                <button class="calc-button">←</button>
                <button class="calc-button">÷</button>
            </div>
            <div class="calc-button-row">
                <button class="calc-button">7</button>
                <button class="calc-button">8</button>
                <button class="calc-button">9</button>
                <button class="calc-button">×</button>
            </div>
            <div class="calc-button-row">
                <button class="calc-button">4</button>
                <button class="calc-button">5</button>
                <button class="calc-button">6</button>
                <button class="calc-button">−</button>
            </div>
            <div class="calc-button-row">
                <button class="calc-button">1</button>
                <button class="calc-button">2</button>
                <button class="calc-button">3</button>
                <button class="calc-button">+</button>
            </div>
            <div class="calc-button-row">
                <button class="calc-button triple">0</button>
                <button class="calc-button">=</button>
            </div>
        </section>
     </div>
    <script src="script.js"></script>
</body>

In script.js I'm using,

function init() {
    document.querySelector('calc-buttons').
        addEventListener('click', function (event) {
            console.log(event, "event in init")
            buttonClick(event.target.innerText);
        })
}
console.log('here')
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标签。

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

document.querySelectorAll('.calc-button')

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

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

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

function init() {
    const buttons = document.querySelectorAll('.calc-button')
    for (let i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener('click', function (event) {
            console.log(event, "event in init")
            buttonClick(event.target.innerText);
        })
    }
}
console.log('here')
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:

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:

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

So, the whole thing would look like this:

function init() {
    const buttons = document.querySelectorAll(&#39;.calc-button&#39;)
    for (let i = 0; i &lt; buttons.length; i++) {
        buttons[i].addEventListener(&#39;click&#39;, function (event) {
            console.log(event, &quot;event in init&quot;)
            buttonClick(event.target.innerText);
        })
    }
}
console.log(&#39;here&#39;)
init();

答案2

得分: 0

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

function init() {
    document.querySelector('.calc-buttons')
        .addEventListener('click', function (event) {
            console.log(event, "init中的事件")
            buttonClick(event.target.innerText);
        });
}

console.log('这里');
init();
英文:

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

function init() {
    document.querySelector(&#39;.calc-buttons&#39;)
        .addEventListener(&#39;click&#39;, function (event) {
            console.log(event, &quot;event in init&quot;)
            buttonClick(event.target.innerText);
        });
}

console.log(&#39;here&#39;);
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:

确定