简体   繁体   English

当我按下一个按钮时,我有 10 个想要的按钮,出现 function

[英]I have 10 buttons that I want when I press a button, a function occurs

I have a page with 10 buttons I want when I press a button the speech inside it changes And div appears, and when you click the second time, everything returns as it was我有一个包含 10 个按钮的页面,当我按下一个按钮时,它里面的语音会发生变化并出现 div,当你第二次单击时,一切都会恢复原样

button 10 html按钮 10 html

<button class="button" id="movetool">click 1</button>
<button class="button" id="movetool2">click 2</button>
<button class="button" id="movetool3">click 3</button>
<button class="button" id="movetool4">click 4</button>
<button class="button" id="movetool5">click 5</button>
<button class="button" id="movetool6">click 6</button>
<button class="button" id="movetool7">click 7</button>
<button class="button" id="movetool8">click 8</button>
<button class="button" id="movetool9">click 9</button>
<button class="button" id="movetool0">click 10</button>

button javacsript按钮 javacsript

var activeButton = localStorage.getItem('activeButton');
var isActive = localStorage.getItem('isActive');
const allButtons = document.querySelectorAll(".button");

[...allButtons].forEach(function(thisButton) {
  if (isActive === 'true') {
    if (activeButton !== thisButton.id) {
      thisButton.disabled = true;

    }
  }
});

const movetool = event => {
  activeButton = localStorage.getItem('activeButton');
  const target = event.target.closest("button");
  if (activeButton === target.id) {
    var enableAll = false;
    Array.from(allButtons).forEach(function(thisButton) {
      if (thisButton.disabled) {
        enableAll = true;
        return;
      }
    });

    if (enableAll) {
      Array.from(allButtons).forEach(function(thisButton) {
        thisButton.disabled = false;
      });

      localStorage.setItem('isActive', false);

    } else {
      Array.from(allButtons).forEach(function(thisButton) {
        thisButton.disabled = true;
      });
      if (target.classList.contains("button")) {
        target.disabled = false;
        localStorage.setItem('isActive', true);
        localStorage.setItem('activeButton', target.id);
      }
    }
  } else {
    Array.from(allButtons).forEach(function(thisButton) {
      thisButton.disabled = true;
    });
    if (target.classList.contains("button")) {
      target.disabled = false;
      localStorage.setItem('isActive', true);
      localStorage.setItem('activeButton', target.id);
    }
  }
}
document.querySelector('.button_container').addEventListener('click', movetool);

I want to change the text when I press the button to "clicked" and show div当我按下按钮以“单击”并显示 div 时,我想更改文本

code代码

the button is clicked 按钮被点击

You should do:你应该做:

allButtons.forEach(button => button.addEventListener("click", () => {
  localStorage.setItem('isActive', button.id)
  
  // Do that refresh loop you had at the top of your question
})

Inside your moveTool event listener callback在您的 moveTool 事件侦听器回调中

thisButton.innerHTML = 'clicked';

暂无
暂无

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

相关问题 我有十个我想要的按钮,当我按下一个按钮时,所有按钮都停止 - I have ten buttons I want when I press a button all buttons stop 我想在提示中按下 Esc 按钮时提醒一个值,但我不知道...(下面的代码) - I want to alert a value when I press the Esc button in prompt, but I have no idea... (code below) 为什么当我按下按钮时,我的 output 会乘以? 我希望它每次点击都有一个 output - Why are my output multiplies when I press the button? I want it to have single output every click 我想建立一种功能,当我按下按钮开始播放另一首歌曲时,该功能可以停止播放歌曲 - I want to build function that stops the playing song when I press the button to start another one 按下按钮时调用功能 - Call a function when I press a button 嘿,当我按下按钮时出现问题我在控制台日志中有一个错误,说我的 function 未定义 - Hey i have a problem when I press the button I have an error in the console log which says my function is not defined 我想在按下打印按钮时得到打印对话框吗? - I want to get print dialog box when press print button? 我正在使用 html 并有两个按钮。 我希望这些按钮有不同的 typeWriter() function 输出 - I'm using html and have two buttons. I want these buttons to have different typeWriter() function outputs 我想“拦截”按下删除按钮 - I want to “intercept” a delete button press 如何使出现其他功能时出现的按钮? - How do I make a button that appears when another function occurs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM