![](/img/trans.png)
[英]Vue.js - I have dynamic buttons and I want that the color changes if I clicked one button
[英]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.