簡體   English   中英

我有 3 個按鈕,我希望其中一個默認處於單擊狀態,除非我單擊 HTML/JS 中的另一個按鈕

[英]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

我希望一個按鈕默認處於單擊狀態,除非我單擊另一個按鈕。 當我單擊另一個按鈕時,一切都應該像普通按鈕一樣工作。

您需要使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM