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

203
Views
El tema oscuro de JS no cambia

Estoy tratando de hacer un tema claro/claro para una mesa en mi proyecto, está oscuro de manera predeterminada, así que estoy tratando de cambiarlo a claro cuando hago clic en el botón, pero cuando hago clic, cambia a claro pero no retrocede. sugerencias?

 var element = document.getElementById('toggleTheme') var attributeContent = element.getAttribute('data-layout-mode') let toggleTheme = document.querySelector(".light-dark-mode"); let styleSheet = document.querySelector("#color-theme"); console.log(attributeContent); if (attributeContent=="dark") { toggleTheme.addEventListener("click", () => { styleSheet.setAttribute("href", "") }) } else if (attributeContent=="light") { toggleTheme.addEventListener("click", () => { styleSheet.setAttribute("href", "{{ asset('css/style.css') }}") }) } else { console.log("not found") }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solo define qué alternar en función del valor inicial del attributeContent .

Es mejor que haga la prueba, sobre si actualmente está oscuro o claro, cada vez que se hace clic en el interruptor.

algo como

 var element = document.getElementById('toggleTheme') let toggleTheme = document.querySelector(".light-dark-mode"); let styleSheet = document.querySelector("#color-theme"); toggleTheme.addEventListener("click", function() { // read the layout mode var attributeContent = element.getAttribute('data-layout-mode'); if (attributeContent == "dark") { styleSheet.setAttribute("href", ""); // set the layout mode to the new value element.setAttribute('data-layout-mode', 'light'); } else if (attributeConten == "light") { styleSheet.setAttribute("href", "{{ asset('css/style.css') }}"); // set the layout mode to the new value element.setAttribute('data-layout-mode', 'dark'); } else { console.log("not found") } })

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!