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

222
Views
¿Cómo puedo mostrar un texto oculto haciendo clic en un botón?

 function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display = "none") { x.style.display = "block"; } else { x.style.display = "none"; } }
 <ul> <li><button onclick="myFunction()">Try it</button> <div id="myDIV"><p>This is my DIV element.</p></div </li> </ul>

En el archivo css incluí el comando: #myDIV{ display: none; } porque de lo contrario el texto no estaría oculto al principio. Con este código el texto se oculta al principio y si hago clic en el botón muestra el texto. Pero luego, si vuelvo a hacer clic en el botón, debería estar cerrando el texto que no es así. ¿Puede alguien ayudarme aquí? ¡Gracias!

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

0

Establezca esto en HTML

 <div id="myDIV" style="display: none;"><p>This is my DIV element.</p></div>

y este javascript

 function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display == "none") { x.style.display = "block"; } else { x.style.display = "none"; } }

Esto ahora funcionará porque la visualización de ninguno no está predeterminada en el elemento div, por lo que javascript no verá el estilo de visualización de ninguno y no ejecutará la función. Por lo tanto, debe especificar el estilo de visualización ninguno y la función funcionará correctamente

about 4 years ago · Juan Pablo Isaza Report

0

= es asignación. == y === son comparaciones. Entonces, dentro del if , cámbielo a uno de esos dos.

Tampoco cerró correctamente el <div> en el código que envió.

Otra cosa es que está configurando la display: none en un archivo CSS. JavaScript no tiene acceso a este valor, solo tiene acceso a style="..." en el marcado HTML del elemento. Para solucionar este problema, compruebe si no está oculto y cambie los lados del bloque if/else .

 function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display == "block") { // `=` to `==` or `===` x.style.display = "none"; } else { x.style.display = "block"; } }
 #myDIV { display: none; }
 <ul> <li><button onclick="myFunction()">Try it</button> <div id="myDIV"> <p>This is my DIV element.</p> </div> <!-- this div wasn't closed properly --> </li> </ul>

about 4 years ago · Juan Pablo Isaza Report

0

 function myFunction() { var x = document.getElementById("myDIV"); x.style.display = x.style.display != "block"?"block":"none" }
 #myDIV{ display: none; }
 <ul> <li><button onclick="myFunction()">Try it</button> <div id="myDIV"><p>This is my DIV element.</p></div </li> </ul>

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!