[英]I have 3 buttons, I want one of them to be in clicked state by default unless I click the other button in HTML/JS
I want one button to be in clicked state by default unless I click on the other button.我希望一个按钮默认处于单击状态,除非我单击另一个按钮。 When I click the other button then everything should work like normal buttons.
当我单击另一个按钮时,一切都应该像普通按钮一样工作。
You need to do something with CSS to emulate the active style, and use JS to trigger that CSS class:您需要使用 CSS 来模拟活动样式,并使用 JS 来触发该 CSS 类:
active.classList.add("active") const btns = document.querySelectorAll("button") btns.forEach((el, i) => { el.onclick = () => { el.classList.add("active") el.innerText = "Active" btns.forEach((el, j) => { if (i !== j) { el.classList.remove("active") el.innerText = "Non Active" } }) } })
.active { border: 1px inset #DDD; background: rgb(245, 245, 245); }
<button id="active">Active</button> <button>Non Active</button> <button>Non Active</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.