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

262
Views
¿Cómo puedo cambiar la imagen en los detalles/resumen de apertura y cierre?

Este es mi primer JavaScript. Tomó mucho tiempo pero no puedo hacerlo funcionar.

Obviamente, lo que debería hacer es cambiar la imagen cuando se abre la parte 'detalles/resumen'. FYI es un seguimiento de este problema que estoy tratando de resolver. ¿Alguien ve lo que estoy haciendo mal?

 <body> <script type="text/javascript"> function changeImage(a) { document.getElementById("img").src = a; } details.addEventListener("toggle", event => { if (details.open) { changeImage("https://via.placeholder.com/50/ff0000"); } else { changeImage("https://via.placeholder.com/50/0000ff"); } }); </script> <details> <summary> <table> <td width="64">#910</td> </table> </summary> <table> <td width="64">#910</td> </table> </details> <img id="img" src='https://via.placeholder.com/50'> </body>

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esa es la parte que falta: const details = document.querySelector('details') . Entonces funcionaría.

 function changeImage(a) { document.getElementById("img").src = a; } const details = document.querySelector('details') details.addEventListener("toggle", event => { if (details.open) { changeImage("https://via.placeholder.com/50/ff0000"); } else { changeImage("https://via.placeholder.com/50/0000ff"); } });
 <body> <script type="text/javascript"> </script> <details> <summary> <table> <td width="64">#910</td> </table> </summary> <table> <td width="64">#910</td> </table> </details> <img id="img" src='https://via.placeholder.com/50'> </body>

over 4 years ago · Santiago Trujillo Report

0

Utilice event.currentTarget; , apunta a la etiqueta que está escuchando el evento. Ese evento es un simple 'clic', nunca leí que 'alternar' era un evento, aprendí algo hoy.

 const changeImage = event => { const clicked = event.currentTarget; const img = document.querySelector('.img'); if (clicked.matches('details')) { if (clicked.open) { img.src = "https://via.placeholder.com/50/ff0000"; } else { img.src = "https://via.placeholder.com/50/0000ff"; } } } document.querySelector('details').addEventListener('click', changeImage)
 <details> <summary> <table> <td width="64">#910</td> </table> </summary> <table> <td width="64">#910</td> </table> </details> <img class="img" src='https://via.placeholder.com/50'>

over 4 years ago · Santiago Trujillo 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!