简体   繁体   English

按钮事件侦听器无响应

[英]Button Event-listener is unresponsive

I'm following a tutorial and I made a button to show some content. 我正在关注一个教程,并且做了一个按钮来显示一些内容。 However this button doesn't work and I'm at my wits end unable to figure out what could be causing this. 但是,此按钮不起作用,我不知所措,无法弄清楚是什么原因引起的。

Can someone show why this doesn't work? 有人可以显示为什么这行不通吗?

 const users = document.querySelector('#user'); const getUsers = document.getElementById('getUsers'); getUsers.addEventListener('click', loadUsers); var loadUsers = () => { console.log('hello button clicked') let xhr = new XMLHttpRequest(); xhr.open('GET', 'https://api.github.com/users', true); xhr.onload = () => { if (this.status == 200) { let gusers = this.responseText console.log(gusers); } } xhr.send() } console.log(getUsers) 
 <h1>USER</h1> <button id="getUsers">Get Users</button> <div id="users"></div> 

Order of your variable declarations matters in this scenario due to hoisting - move the loadUsers definition above the call. 在这种情况下,由于吊装 ,变量声明的顺序很重要-将loadUsers定义移到调用上方。

JavaScript only hoists declarations, not initializations. JavaScript仅提升声明,而不提升初始化。 If a variable is declared and initialized after using it, the value will be undefined. 如果在使用变量后声明并初始化了变量,则该值将是不确定的。

The block-quote above from MDN explains why function declarations can be defined after they are called (reading code from top-to-bottom), but variables that are initialized after they are used would have a value of undefined . 块引号以上从MDN解释了为什么他们被称为(从顶部至底部读码) 函数声明可以被定义,但它们被使用之后将具有一个值,该值被初始化的变量undefined

 const users = document.querySelector('#user'); const getUsers = document.getElementById('getUsers'); const loadUsers = () => { console.log('Load users..'); } getUsers.addEventListener('click', loadUsers); 
 <head> <meta charset="UTF-8"> <title>Testing AJAX</title> </head> <body> <h1>USER</h1> <button id="getUsers">Get Users</button> <div id="users"></div> </body> 

Or you could keep the function at the bottom but use a function declaration which will be hoisted: 或者,您可以将函数保留在底部,但是使用将被悬挂的函数声明:

 const users = document.querySelector('#user'); const getUsers = document.getElementById('getUsers'); getUsers.addEventListener('click', loadUsers); function loadUsers() { console.log('Load users..'); } 
 <head> <meta charset="UTF-8"> <title>Testing AJAX</title> </head> <body> <h1>USER</h1> <button id="getUsers">Get Users</button> <div id="users"></div> </body> 

In addition to the correct answer have a look at your code that I have refactored below. 除了正确的答案之外,请查看下面重构的您的代码。 Hope this helps. 希望这可以帮助。

 // Get Elements const usersList = document.querySelector('#usersList'); const usersBtn = document.querySelector('#usersBtn'); // Bind listener to usersButton usersBtn.addEventListener('click', () => { // XHR Request function const xhr = new XMLHttpRequest(); xhr.open('GET','https://api.github.com/users') xhr.send() xhr.onload = function() { if (xhr.status == 200) { // Convert the response to JSON and assign it to data const data = JSON.parse(xhr.responseText) // Loop throug through data for(let i = 0; i <data.length; i++) { // Create LI element and append the user name const listItem = document.createElement('li'); usersList.appendChild(listItem).innerHTML = data[i].login } } } }) 
 <h1>USERS</h1> <button id="usersBtn">Get Users</button> <ul id="usersList"></ul> 

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

相关问题 Javascript如何用事件监听器移动变量? - Javascript how to move variables with event-listener? 关于问答游戏中的事件监听器 - About an event-listener inside a quiz game iOS Safari:“ mousemove”上有事件监听器时不会触发“ click”事件 - iOS Safari: 'click' event not triggerd when there is an event-listener on 'mousemove' 如何从事件侦听器中排除向下箭头键 - How to exclude down arrow key from event-listener Next.js:将“样式”添加到事件侦听器中的元素 - Next.js: Add 'style' to element in event-listener 向使用标记的Google地理图表添加事件监听器 - Add an event-listener to a google geochart which uses markers 在JavaScript中触发单个事件监听器的多个动作(单击) - Triggering multiple actions for single event-listener(on-click) in javascript 如果调用Javascript事件侦听器并且缺少目标元素,会发生什么情况? - What happens if a Javascript event-listener is called and target element is missing? 有没有办法在 Javascript 中获取所有事件侦听器绑定? - Is there a way to get all event-listener bindings in Javascript? Javascript(Dojo):销毁对象时是否删除事件监听器 - Javascript (Dojo): Is event-listener removed when object is destroyed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM