AddEventListener到按钮的问题

发布于 2025-02-01 18:04:40 字数 1193 浏览 5 评论 0原文

我正在创建一个简单的随机用户应用程序,每次点击按钮时,它都会显示新的用户信息,我的下一个按钮不起作用,我不知道我在做什么错即将在DOM加载之前按钮。

const apiUrl = `https://randomuser.me/api`;
const nextBtn = document.querySelector('button');



async function getUser(){

    const res = await fetch(apiUrl);
    const data = await res.json();
    console.log(data.results[0]);
    let results = data.results[0];
    displayUser(results)
}

function displayUser(results){

const userContainer = document.createElement('div');
userContainer.classList.add('user-container');
userContainer.innerHTML = `

<img src="${results.picture.large}" alt="">
        <div class="title">
            <h3>${results.name.first}</h3>
            <h3>${results.name.last}</h3>
        </div>
        <div class="info">
            <h3>${results.location.street.number + ' ' +results.location.street.name}</h3>
            <h3>${results.phone}</h3> 
            <h3>${results.email}</h3>
        </div>
        <button>Next User</button>
       
`;

document.body.appendChild(userContainer);
}

getUser();

nextBtn.addEventListener('click', getUser);

I am creating a simple random user app that every time that the button is clicked, it will show the new user information, my next button doesn't work, i have no idea what i am doing wrong but i think I'm calling the button even before The DOM loads.

const apiUrl = `https://randomuser.me/api`;
const nextBtn = document.querySelector('button');



async function getUser(){

    const res = await fetch(apiUrl);
    const data = await res.json();
    console.log(data.results[0]);
    let results = data.results[0];
    displayUser(results)
}

function displayUser(results){

const userContainer = document.createElement('div');
userContainer.classList.add('user-container');
userContainer.innerHTML = `

<img src="${results.picture.large}" alt="">
        <div class="title">
            <h3>${results.name.first}</h3>
            <h3>${results.name.last}</h3>
        </div>
        <div class="info">
            <h3>${results.location.street.number + ' ' +results.location.street.name}</h3>
            <h3>${results.phone}</h3> 
            <h3>${results.email}</h3>
        </div>
        <button>Next User</button>
       
`;

document.body.appendChild(userContainer);
}

getUser();

nextBtn.addEventListener('click', getUser);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2025-02-08 18:04:40

创建按钮时,您需要将事件绑定到按钮。该代码不会继续寻找要添加的按钮并将事件添加到其中。

function add() {
  const userContainer = document.createElement('div');
  userContainer.classList.add('user-container');
  userContainer.innerHTML = `<button>Add</button>`;
  userContainer.querySelector('button').addEventListener('click', add);
  document.body.append(userContainer);

}
add();

You need to bind the event to the button when you create the button. The code does not keep looking for buttons to be added and add events to it.

function add() {
  const userContainer = document.createElement('div');
  userContainer.classList.add('user-container');
  userContainer.innerHTML = `<button>Add</button>`;
  userContainer.querySelector('button').addEventListener('click', add);
  document.body.append(userContainer);

}
add();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文