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

199
Views
seleccione una sola imagen al hacer clic en la etiqueta <img>

He renderizado varias imágenes usando tag. Quiero cambiar el color de fondo de la imagen cuando se hace clic en ella. Pero cuando hago clic en una imagen, el color de todas las imágenes cambia.

Aquí está mi código. Estoy trabajando en Angular.

html

 <img [src]="data.Url" alt="image" (click)="_upload(data.Url)" [ngClass]="{'green' : toggle, 'red': !toggle}" >

t

dentro de mi funcion

 this.toggle = !this.toggle; this.status = this.toggle ? 'Enable' : 'Disable';
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Para renderizar múltiples elementos html, supongo que está usando la directiva ngFor , por lo que podría usar el index para dar a cada elemento una clase css diferente.

Por ejemplo:

HTML

 <div *ngFor="let option of options; let i = index"> <img [src]="option.Url" (click)="_upload(option.Url, i)" [class.selected]="i === indexSelected" <!--apply the selected css class if condition is TRUE--> > </div>

CSS

 .selected { background-color: green; }

t

 ... export class YourComponent { indexSelected: number = -1; _upload(url: string, index: number) { // ... this.indexSelected = index; } }
about 4 years ago · Juan Pablo Isaza Report

0

Gracias a todos lo hice de esta manera

 <td *ngFor="let data of responseData; let i= index;" (click)="active= i" [ngStyle]="{'background-color': active === i ? '#4b8df2' : 'white' }"> <div> <img [src]="data.Url" alt="image" (click)="_upload(data.Url)" > </div> </td>
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!