简体   繁体   English

如何仅使用 Angular6 在单击的按钮上设置活动类?

[英]How to set active class on clicked button only using Angular6?

I have 3 buttons in a row.我连续有 3 个按钮。 I want to set active class on clicked button only but problem is when i clicked another button after one button so it is setting active class on all buttons.我只想在单击的按钮上设置活动类,但问题是当我在一个按钮后单击另一个按钮时,它会在所有按钮上设置活动类。

Set active class is not working correctly.设置活动类工作不正常。 All buttons get active class when clicked.单击时,所有按钮都会获得活动类。 If i will click on 2 buttons it will set active class on both button when clicked.如果我将点击 2 个按钮,它会在点击时在两个按钮上设置活动类。

Here is my code at stackblitz这是我在 stackblitz 上的代码

StackBlitz Code堆栈闪电战代码

i want to set active class only on clicked button at a time not on all the buttons.我想一次只在单击的按钮上设置活动类,而不是在所有按钮上。

I think that you can do this more easy, with angular dom features.我认为您可以使用 angular dom 功能更轻松地做到这一点。

You can show here:您可以在此处显示:

https://stackblitz.com/edit/angular-s8suc8?file=src/app/app.component.ts https://stackblitz.com/edit/angular-s8suc8?file=src/app/app.component.ts

it is a sample that I think that you want, but I think that you can do it better:这是我认为您想要的示例,但我认为您可以做得更好:

https://stackblitz.com/edit/angular-vmzijo?file=src/app/app.module.ts https://stackblitz.com/edit/angular-vmzijo?file=src/app/app.module.ts

as far as i can understand you need to toggle on active and inactive status:据我所知,您需要切换活动和非活动状态:

This is the new plunker , have a look if it solves the issue:这是新的 plunker ,看看它是否解决了问题:

https://stackblitz.com/edit/angular-fzxhkv?file=src/app/app.component.ts https://stackblitz.com/edit/angular-fzxhkv?file=src/app/app.component.ts

I have removed event.srcElement.classList.add('phase-active') which was responsible for applying phase-active on every button我已经删除了event.srcElement.classList.add('phase-active')它负责在每个按钮上应用phase-active

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular:仅在当前单击的按钮上切换活动类(不使用 *ngFor) - Angular: Toggle active class on only button the current clicked button (not using *ngFor) 仅在单击专用按钮时如何激活功能 - How to active a function only if dedicated button is clicked 如果在子按钮上将“活动”类添加到“此”父级,则单击该按钮并在再次单击按钮时切换“活动”类 - How to add “active” class to “this” parent on child button is clicked and toggle “active” class if button clicked again 将活动 class 分配给单击的按钮 - Assign active class to the clicked button 使用 Angular6 在 3 个元素之间切换类 - Toggle class between 3 elements using Angular6 将活动类分配给使用For循环创建的Clicked按钮 - Assign a active class to Clicked button created using For loop 如何仅获取在 JavaScript 中具有相同 class 的一组按钮中单击的一个按钮的值? - How to get value of only one button that was clicked in a set of buttons that have the same class in JavaScript? 仅在单击的按钮上应用活动状态 - Apply active state on clicked button only if 语句在单击按钮时检查活动 class - if statement check active class when button is clicked class 在 JS 处于活动状态时再次单击后如何从按钮中删除活动 - how can i remove class active from a button after getting clicked again while it's active with JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM