Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

291
Visualizações
cómo guardar la clase de toogle con almacenamiento local. Entonces, ¿alguien puede verificar qué está mal con este código?

 if( localStorage.getItem("color") == "black" ) { { var element = document.getElementById("body"); element.classList.toggle("bdark"); } { var element = document.getElementById("theader"); element.classList.toggle("hdark"); } { var element = document.getElementById("sh"); element.classList.toggle("shh"); } } function myFunction() { { var element = document.getElementById("body"); element.classList.toggle("bdark"); } { var element = document.getElementById("theader"); element.classList.toggle("hdark"); } { var element = document.getElementById("sh"); element.classList.toggle("shh"); } var hs = document.getElementById("hs"); var color; if(localStorage.getItem("color") == "black") { color = "black"; localStorage.setItem("color",color) }
 .bdark { background-color: #333; color: white; } .hdark { background-color: black; color: white; } .shh { display: none; } .hs { display: none; }
 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body id="body" class="light"> <p id="theader">Click the "Try it" button to toggle between adding and removing the "mystyle" class name of the DIV element:</p> <button id="button" onclick="myFunction()">Try it</button> <div id="aaas"> <div id="sh" class="sh">&#9790;</div> <div id="hs" class="hs">&#9728;</div> </div> </body> </html>

Quiero que este código haga clic en alternar clase y cuando actualice la página, la clase alternada permanecerá igual que antes de volver a cargar la página con almacenamiento local. Entonces, ¿alguien puede verificar qué está mal con este código? ayúdame con algo similar/alternativo a este. gracias por leer esto

Detalles:-

Quiero que este código funcione como (cambio de clase al hacer clic + guardado con cokies/almacenamiento local/o cualquier cosa), de modo que cada vez que actualice o vuelva a abrir la página, será de la misma clase que cuando me fui. o algún código alternativo que funcione igual.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Arreglé tu código

 if( localStorage.getItem("color") == "black" ) { { let element = document.getElementsByTagName("body"); element.classList.toggle("bdark"); } { let element = document.getElementById("theader"); element.classList.toggle("hdark"); } { let element = document.getElementById("sh"); element.classList.toggle("shh"); } } function myFunction() { { let element = document.getElementsByTagName("body"); element.classList="bdark"; } { let element = document.getElementById("theader"); element.classList="hdark"; } { let element = document.getElementById("sh"); element.classList="shh"; } { let hs = document.getElementById("hs"); } let color; if(localStorage.getItem("color") != "black") { color = "black"; localStorage.setItem("color", color) } }
 .bdark { background-color: #333; color: white; } .hdark { background-color: black; color: white; } .shh { display: none; } .hs { display: none; }
 <p id="theader">Click the "Try it" button to toggle between adding and removing the "mystyle" class name of the DIV element:</p> <button id="button" onclick="myFunction()">Try it</button> <div id="aaas"> <div id="sh" class="sh">&#9790;</div> <div id="hs" class="hs">&#9728;</div> </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda