Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
OnClick comprueba, elimina y agrega el nombre de la clase en angular13

Quiero agregar el nombre de clase 'activo' onClick a 'li a' y también eliminar cualquier clase 'activa' presente en 'li a'. El código actual funciona correctamente si hago clic en la secuencia desde los elementos superiores, pero cuando hago clic en los elementos de abajo hacia arriba, no funciona.

  1. componente.html
 <div class="container text-center"> <ul id="myList" class="pt-5"> <li class="p-3"> <a href="#" class="d-inline-block" (click)="linkActive($event)">List 1</a> </li> <li class="p-3"> <a href="#" class="d-inline-block" (click)="linkActive($event)">List 2</a> </li> </ul> </div>
  1. componente.ts
 linkActive(event) { const activeClass = event.srcElement.classList.contains('active'); const classFound = document.querySelector('li a'); const hpn = classFound.classList.contains('active'); if (activeClass == true) { if (hpn == true) { classFound.classList.remove('active'); } alert('true'); event.srcElement.classList.remove('active'); } else { if (hpn == true) { classFound.classList.remove('active'); } alert('false'); event.srcElement.classList.add('active'); } }

Encuentre el código de muestra: https://stackblitz.com/edit/angular-ivy-jf9xvp

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No manipule el DOM directamente de esta manera. Puede usar ngClass para lograr el resultado deseado:

modelo:

 <a href="#" class="d-inline-block" [ngClass]="{'active': activeList === 1}" (click)="linkActive(1)">List1</a>

.ts

 public activeList: number;

...

 public linkActive(listNumber: number) { this.activeList = listNumber; }

En general, como se recomienda en los comentarios, haga el tutorial de héroes e intente comprender cómo usar mecanografiado.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!