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

364
Views
Modo oscuro y modo claro html, css, javascript ¿cómo?

Hola y gracias de antemano, me gustaría hacer un botón con modo oscuro y si haces clic en él, se convierte en modo oscuro y el botón se llama modo blanco, luego, cuando vuelves a hacer clic en él, se convierte en modo blanco y el botón se llama Modo oscuro de nuevo.

Código:

 <!DOCTYPE html> <html> <head> <style> .page { padding: 25px; background-color: white; color: black; font-size: 25px; } .dark-mode { background-color: black; color: white; } </style> </head> <body> <div class="page">Hello</div> <button onclick="myFunction()">dark mode</button> <script> function myFunction() { var element = document.page; element.classList.toggle("dark-mode"); } </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Está intentando alternar la clase dark-mode en document.page , que no existe.

En su lugar, debería alternarlo en document.body .

 <!DOCTYPE html> <html> <head> <style> .page { padding: 25px; background-color: white; color: black; font-size: 25px; } .dark-mode { background-color: black; color: white; } </style> </head> <body> <div class="page">Hello</div> <button onclick="myFunction()">dark mode</button> <script> function myFunction() { var element = document.body; element.classList.toggle("dark-mode"); } </script> </body> </html>

Para alternar solo la clase en el div , use querySelector :

 <!DOCTYPE html> <html> <head> <style> .page { padding: 25px; background-color: white; color: black; font-size: 25px; } .dark-mode { background-color: black; color: white; } </style> </head> <body> <div class="page">Hello</div> <button onclick="myFunction()">dark mode</button> <script> function myFunction() { var element = document.querySelector('div.page'); element.classList.toggle("dark-mode"); } </script> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

 function myFunction() { var element = document.body; element.classList.toggle("dark-mode"); if (element.classList.contains("dark-mode")){ document.getElementById('btn').innerHTML = "Light Mode"; } else{ document.getElementById('btn').innerHTML = "Dark Mode"; } }
 .page { display:inline; background:#FFF; color: #121212; font-size: 25px; } .dark-mode, .dark-mode .page { background-color:#202020; color: #FFF; }
 <!DOCTYPE html> <html> <head> </head> <body> <div class="page">Hello</div> <br><br><br> <button onclick="myFunction()" id="btn">Dark mode</button> </body> </html>

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!