简体   繁体   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

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.

相关问题 Vue.js - 我有动态按钮,如果我点击一个按钮,我希望颜色会改变 - Vue.js - I have dynamic buttons and I want that the color changes if I clicked one button 我有一个动态 html 模板。 当我单击一个按钮时,哪个 forms 。 现在,问题是当我单击一个按钮时,所有按钮都被单击 - I have a dynamic html template. Which forms when I click on a button. Now, the problem is When I click on one button all buttons are clicked 我正在尝试从node.js中的其他套接字隐藏按钮。 单击按钮后,我是一个玩家,我不希望其他玩家能够单击它 - I'm trying to hide a button from other sockets in node.js. After the buttons is clicked my a player i don't want other players to be able to click it 我在一个组件中同时使用两个按钮,当我单击它而不是另一个按钮时,我只希望一个按钮起作用 - i am using two buttons simultaneously in one component, i want only one button to work when i click it not the other button 我有 4 个下拉列表,我想在页面加载时隐藏它们,然后每当单击添加按钮时,就会出现一个下拉列表 - I have 4 dropdown and I want to hide them when page loads and then whenever add button is clicked then one dropdown should appear 当我单击加号按钮时,我有两个按钮,网页必须放大,然后单击减号按钮以缩小。单击其他按钮以默认 - I have two buttons when i click on plus button the web page has to zoom in and click on minus button to zoom out .click on other button to default 我想在HTML中彼此重叠两个文本区域,然后单击按钮切换它们的堆叠顺序 - I want to have two textareas on top of one another in HTML, and switch the order they are stacked, with click of a button 我希望按钮在单击时改变颜色吗? - Do I want the buttons to change color when I click on them? 当我单击特定的“addToBasket”按钮时,我不想更改所有“addToBasket”按钮。 只有点击的按钮应该改变 - I do not want all the "addToBasket" buttons to change when i click on a specefic "addToBasket" button. Only the clicked button should change 我想创建点击的脸被标记,其他脸是第二次点击的默认颜色 - i want to create clicked face is marked and other faces are default color on second click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM