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

197
Views
html mostrar ocultar elemento div

Estoy tratando de show/hide div en html ahora estoy usando w3schools Toggle Hide and Show link . Está funcionando bien, pero de forma predeterminada muestra la condición que, de forma predeterminada, oculta la condición si presiono el botón Mostrar, debería mostrarse. ayúdame.

código

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> #myDIV { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } </style> </head> <body> <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p> <button onclick="myFunction()">Try it</button> <div id="myDIV"> This is my DIV element. </div> <p><b>Note:</b> The element will not take up any space when the display property set to "none".</p> <script> function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Debe realizar los siguientes cambios en su código. Agregue una display:none para #myDIV en css para ocultarlo en la carga inicial. Pero si aún verifica en JS (x.style.display), obtendrá una cadena vacía en lugar de ninguna. Eso es porque el atributo de estilo no tiene ninguna configuración para mostrar hasta que el código se ejecuta por primera vez. Entonces, para manejar este caso, debe actualizar su condición if también de esta manera: if (x.style.display == "none" || x.style.display === "")

Puede consultar aquí para obtener más información - Enlace de pregunta similar

He adjuntado un fragmento. Espero que así es como quieres que funcione.

 function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display == "none" || x.style.display === "") { x.style.display = "block"; } else { x.style.display = "none"; } }
 #myDIV { width: 100%; display: none; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; }
 <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p> <button onclick="myFunction()">Try it</button> <div id="myDIV"> This is my DIV element. </div> <p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

about 4 years ago · Juan Pablo Isaza Report

0

Agregar pantalla: ninguno al div css,

 #myDIV { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; display: none; }

y cambie la condición if a esto:

 if (x.style.display == "none" || x.style.display === "")
about 4 years ago · Juan Pablo Isaza Report

0

Sugiero no actualizar la propiedad de estilo directamente, sino alternar una clase "oculta" en su lugar. Aquí hay un fragmento:

 function myFunction() { const x = document.getElementById("myDIV"); x.classList.toggle("hidden"); }
 #myDIV { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } .hidden { display: none; }
 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p> <button onclick="myFunction()">Try it</button> <div id="myDIV" class="hidden"> This is my DIV element. </div> </body> </html>

Si esto no resuelve su problema, hágamelo saber;)

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!