[英]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代码
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.