简体   繁体   English

为什么我收到 null 的“无法读取属性”“addEventListener”

[英]Why am i getting 'Cannot read property 'addEventListener' of null

I wanted to make an app where I can show/hide my students data by clicking on the names.我想制作一个应用程序,我可以通过单击名称来显示/隐藏我的学生数据。 I used addEventListener where I click on the names, then it'll add a class where it will show the students data.我使用addEventListener单击名称,然后它会添加一个 class ,它将显示学生数据。 But I am getting this error Uncaught TypeError: Cannot read property 'addEventListener' of null但我收到此错误Uncaught TypeError: Cannot read property 'addEventListener' of null

Anybody knows why?有人知道为什么吗? I appreciate the help.我很感激帮助。 Thank you.谢谢你。

This is my HTML code:这是我的 HTML 代码:

<div class="container">
        <div class="hero">
            <h2>Students Data</h2>
        </div>

        <div id="output"></div>
</div>

The js code: js代码:

const output = document.getElementById('output')
const studentsName = document.querySelector('.students-name');
const studentsData = document.querySelector('.students-data');

const studentsList = [
    {
        name: 'John',
        january: 'Approved',
        february: 'Approved',
    },
    {
        name: 'Mike',
        january: 'Not Approved',
        february: 'Approved',
    },
    {
        name: 'Greg',
        january: 'Approved',
        february: 'Not Approved',
    },
    {
        name: 'Ash',
        january: 'Not Approved',
        february: 'Not Approved',
    }
]

const displayStudents = studentsList.map((student) => {
    return `
        <div class="students-name">
            <h3>${student.name}</h3>
            <div class="students-data">
                <li>January: ${student.january}</li>
                <li>February: ${student.february}</li>
            </div>
        </div>
    `
})
output.innerHTML = displayStudents.join('')

studentsName.addEventListener('click', () => {
    studentsData.classList.add('show');
})

And this is the css:这是 css:

.students-data{
    display: none;
}

.show{
    display: block;
}

Your order of operations is off.您的操作顺序已关闭。 You are defining your const for the student name and data before you place it into the dom.在将学生姓名和数据放入 dom 之前,您正在为学生姓名和数据定义 const。 Also, you should run these through querySelectorAll method so you can get each nodelist for iteration on click as querySelector will only return the first element in the document that matches the specified set of CSS selectors.此外,您应该通过querySelectorAll方法运行这些,以便您可以在单击时获取每个节点列表以进行迭代,因为 querySelector 将仅返回文档中与指定的 CSS 选择器集匹配的第一个元素。 You can then use the key in the loop to get the data info for each name you click on, then toggle the elements class that shows it.然后,您可以使用循环中的键来获取您单击的每个名称的数据信息,然后切换显示它的元素 class。

 const output = document.getElementById('output') const studentsList = [{ name: 'John', january: 'Approved', february: 'Approved', }, { name: 'Mike', january: 'Not Approved', february: 'Approved', }, { name: 'Greg', january: 'Approved', february: 'Not Approved', }, { name: 'Ash', january: 'Not Approved', february: 'Not Approved', } ] const displayStudents = studentsList.map((student) => { return ` <div class="students-name"> <h3>${student.name}</h3> <div class="students-data"> <li>January: ${student.january}</li> <li>February: ${student.february}</li> </div> </div> ` }) // here is where your innerHTML that includes the classes for // student-data and student-name // are being inserted into the DOM output.innerHTML = displayStudents.join(''); // now you can declare the variables that select the elements classes const studentsName = document.querySelectorAll('.students-name'); const studentsData = document.querySelectorAll('.students-data'); studentsName.forEach((student, i) => { student.addEventListener('click', () => { studentsData[i].classList.toggle('show'); }) })
 .students-data { display: none; }.show { display: block; }
 <div class="container"> <div class="hero"> <h2>Students Data</h2> </div> <div id="output"></div> </div>

First, wrap your js code inside an onload event, the data will be ready when you start running your selectors:首先,将您的 js 代码包装在onload事件中,当您开始运行选择器时,数据将准备就绪:

window.onload = (event) => {
  // js code
}

Then move your studentsName selectors after you add the student section with innerHTML :然后在使用innerHTML添加学生部分后移动你的studentsName选择器:

output.innerHTML = displayStudents.join("");

const studentsName = document.querySelector(".students-name");
const studentsData = document.querySelector(".students-data");

demo: https://www.w3schools.com/code/tryit.asp?filename=GNSPS3ZG5KL0演示: https://www.w3schools.com/code/tryit.asp?filename=GNSPS3ZG5KL0

暂无
暂无

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

相关问题 为什么我无法读取 null javascript 的属性“addEventListener” - why i am getting Cannot read property 'addEventListener' of null javascript Javascript:为什么我得到这个Uncaught TypeError:无法读取null的属性&#39;addEventListener&#39;? - Javascript: Why am I getting this Uncaught TypeError: Cannot read property 'addEventListener' of null? 为什么会出现“无法读取null的属性样式”错误? - Why I am getting “cannot read property style of null” error? 为什么我无法读取 null 错误的属性“addEventListener”? - Why i have Cannot read property 'addEventListener' of null error? 无法读取 null 的 addEventListener 的属性 - Cannot Read the property of addEventListener of null 无法读取 null 的属性“addEventListener” - Cannot read property 'addEventListener' of null 无法读取 null 的属性 addEventListener - Cannot read property addEventListener of null 获取“未捕获的 TypeError:无法读取属性 'addEventListener' of null” - Getting a "Uncaught TypeError: Cannot read property 'addEventListener' of null" 我正在使用focusin / focusout,并且得到:未捕获的TypeError:无法读取null的属性&#39;addEventListener&#39; - I'm using focusin/ focusout and I'm getting: Uncaught TypeError: Cannot read property 'addEventListener' of null 为什么我的脚本中出现“未捕获的TypeError:无法读取null的属性&#39;nodeName&#39;”? - Why am I getting “Uncaught TypeError: Cannot read property 'nodeName' of null” in my script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM