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

133
Views
hacer createElement un enlace en el atributo IMG

he creado un elemento en javascript que muestra una imagen:

 const image = document.createElement('img'); image.setAttribute('class', 'imgGame'); image.setAttribute('alt', game.name ); image.setAttribute('title', game.name ); image.src = game.background_image; card.appendChild(image);

ahora he tratado de agregar

image.setAttribute('href', mylinkVariable);

o

 image.href =

pero no puedo convertir la imagen en un enlace (objetivo en blanco y nofollow) a un sitio web.

¿Qué estoy haciendo mal?

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

0

envuelva el <img> con un <a> y luego agregue el href.

o

 img.onclick = () => window.location.replace('YOUR URL');
about 4 years ago · Juan Pablo Isaza Report

0

No puede agregar un enlace a img, solo envuélvalo con una etiqueta (enlace) como esta;

 const card = document.getElementById('card'); const link = document.createElement('a'); const image = document.createElement('img'); image.setAttribute('class', 'imgGame'); image.setAttribute('alt', game.name); image.setAttribute('title', game.name); image.src = game.background_image; link.appendChild(image); link.setAttribute('href', mylinkVariable); card.appendChild(link);
about 4 years ago · Juan Pablo Isaza Report

0

Adjunte un detector de eventos de click a la imagen que asigne la ubicación deseada a window.location cuando se haya hecho clic en la imagen:

 const image = document.createElement('img'); image.setAttribute('class', 'imgGame'); image.src = 'https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1'; document.body.appendChild(image); image.addEventListener('click', () => location = "https://stacksnippets.net")

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!