简体   繁体   English

如何在一个函数中为不同的按钮添加多个事件监听器?

[英]how to add multiple eventListeners in one function for different buttons?

const nbaLoads = () => {
  fetch('nba.json')
  .then(res => res.json())
  .then(data => {

    let output = '';

    data.forEach(s => {
        if(s.year === parseInt(select.value)){
         output += `
        <img src="" width= 100px; height=100px>
        <h4>CHAMPIONS: ${s.champion}</h4>
      `
    } if(s.year === parseInt(select.value)){
        output += `
        <img src="" width= 100px; height=100px>
        <h4>RUNNER UP: ${s.runnerUp}</h4>
      `
    }
    });
    document.querySelector('#output').innerHTML = output;
  })
};

champion.addEventListener('click', nbaLoads);
runnerUp.addEventListener('click', nbaLoads);

if i click on champions button my result is champions and runner up if i click on runner up button its same, how to change it?如果我点击冠军按钮我的结果是冠军和亚军如果我点击亚军按钮它相同,如何改变它? i want to see champions when i click on champion button and runnerUp when i click runnerUp.我想在我点击冠军按钮时看到冠军,当我点击亚军时看到亚军。

thanks in advance提前致谢

You are calling the same function on both buttons click.您在单击两个按钮时调用相同的函数。 You need either provide an argument which should indicate what type of information should you set to innerHTML , or create two different functions.您需要提供一个参数来指示您应该将什么类型的信息设置为innerHTML ,或者创建两个不同的函数。

First solution:第一个解决方案:

const nbaLoads = (type) => {
  fetch('nba.json')
  .then(res => res.json())
  .then(data => {

    let output = '';

    data.forEach(s => {
        if(s.year === parseInt(select.value) && type == "champions"){
         output += `
        <img src="" width= 100px; height=100px>
        <h4>CHAMPIONS: ${s.champion}</h4>
      `
    } if(s.year === parseInt(select.value) && type == "runnerUp"){
        output += `
        <img src="" width= 100px; height=100px>
        <h4>RUNNER UP: ${s.runnerUp}</h4>
      `
    }
    });
    document.querySelector('#output').innerHTML = output;
  })
};

champion.addEventListener('click', () => nbaLoads("champions"));
runnerUp.addEventListener('click', () => nbaLoads("runnerUp"));

Second solution:第二种解决方案:

const nbaLoads = () =>
  fetch('nba.json')
  .then(res => res.json());

const loadChampion = () =>
  nbaLoads()
  .then(data => {
    let output = '';

    data.forEach(s => {
      if (s.year === parseInt(select.value)) {
        output += `
        <img src="" width= 100px; height=100px>
        <h4>CHAMPIONS: ${s.champion}</h4>
      `;
      }
    });

    document.querySelector('#output').innerHTML = output;

  });

const loadRunnerUp = () =>
  nbaLoads()
  .then(data => {
    let output = '';

    data.forEach(s => {
      if (s.year === parseInt(select.value)) {
        output += `
        <img src="" width= 100px; height=100px>
        <h4>RUNNER UP: ${s.runnerUp}</h4>
      `;
      }

      document.querySelector('#output').innerHTML = output;

    });
  });

champion.addEventListener('click', loadChampion);
runnerUp.addEventListener('click', loadRunnerUp);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM