简体   繁体   中英

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:

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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