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

480
Views
El botón HTML onclick requiere dos clics

Vi las respuestas de preguntas similares pero no me ayudaron

Tengo un botón que, al hacer clic, cambiará el color de fondo del botón y ocultará/mostrará parte del contenido

HTML

 <button class="pro-saved-team-btn" id="{{ teams[loop.index0] | replace(' ', '') + 'button' }}" onclick="show_team('{{ team | replace(' ', '') }}', '{{ team | replace(' ', '') + 'button' }}')"> {{ team.title() }} </button>

JS en HTML

 <script> function show_team(team, team_btn) { x = document.getElementById(team); btn = document.getElementById(team_btn); if (x.style.display === "none") { x.style.display = "block"; btn.style.backgroundColor = '#1DA1F2'; } else { x.style.display = "none"; btn.style.backgroundColor = '#161616'; } } </script>

Sé que la función funciona, pero necesito hacer clic dos veces en el botón para que cambie el color de fondo.

¿Alguien sabe cómo arreglar esto para que funcione después de un solo clic?

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

0

El problema es que cuando las cosas se inician, nadie ha establecido el estilo de visualización de propiedades.

Por lo tanto, la primera vez que la prueba x.style.display === "none" falla.

Entonces, el código establece x.style.display en 'ninguno'.

Entonces, la próxima vez, la prueba funciona.

Para evitar tener que configurar cada visualización de estilo en ninguno en el cambio de inicio

 x.style.display === "none"

a:

 x.style.display != "block"
about 4 years ago · Juan Pablo Isaza Report

0

El problema no se reproduce con el fragmento de código que ha proporcionado:

 <html> <html> <head> </head> <body> <div id="teamId"> hee hee hee </div> <button id="buttonId" onclick="show_team('teamId','buttonId')"> click </button> <script> function show_team(team, team_btn) { x = document.getElementById(team); btn = document.getElementById(team_btn); if (x.style.display === "none") { x.style.display = "block"; btn.style.backgroundColor = '#1DA1F2'; } else { x.style.display = "none"; btn.style.backgroundColor = '#161616'; } } </script> </body> </html>

Puedes hacer lo siguiente:

  1. agregue registros ( console.log('clicked') ) a su función para asegurarse de que cada clic que haga sea recibido por el botón, e investigue la fuente de las intercepciones si algunos clics nunca llegaron al botón
  2. averigüe si hay otros lugares en su código que hagan algo con estos botones e ID de equipo
  3. elimine la clase pro-saved-team-btn onclick botón y verifique si cambia el comportamiento del botón al hacer clic
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!