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

182
Visualizações
TypeError no capturado: no se pueden leer las propiedades de undefined (leyendo 'setItem')

Quiero crear un sistema simple de modo claro/oscuro con javascript. Funciona configurando el booleano 'darkTheme' en Window.localStorage Aquí está el código que escribí:

 let lightThemeCheckbox = document.getElementById('lightThemeCheckbox'); let body = document.body; updateLightTheme(); lightThemeCheckbox.addEventListener('change', function() { Window.localStorage.setItem('darkTheme', this.checked); }); function updateLightTheme() { let darkTheme = window.localStorage.getItem('darkTheme'); if(darkTheme == undefined) { body.style.backgroundColor = 'rgb(255, 255, 255)'; return; } body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)'; }

Pero por alguna razón, me sale este error:

 Uncaught TypeError: Cannot read properties of undefined (reading 'setItem') at HTMLInputElement.<anonymous> (header.js:7) (anonymous) @ header.js:7

¿Alguien puede ayudar?

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

0

Has puesto "ventana" con w mayúscula. Puedes usar lo siguiente:

Usando window minúsculas

 const lightThemeCheckbox = document.getElementById('lightThemeCheckbox'); const body = document.body; updateLightTheme(); lightThemeCheckbox.addEventListener('change', () => { window.localStorage.setItem('darkTheme', this.checked); }); function updateLightTheme() { const darkTheme = window.localStorage.getItem('darkTheme'); if (darkTheme == undefined) { body.style.backgroundColor = 'rgb(255, 255, 255)'; return; } body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)'; }

Usando localStorage sin window

 const lightThemeCheckbox = document.getElementById('lightThemeCheckbox'); const body = document.body; updateLightTheme(); lightThemeCheckbox.addEventListener('change', () => { localStorage.setItem('darkTheme', this.checked); }); function updateLightTheme() { const darkTheme = localStorage.getItem('darkTheme'); if (darkTheme == undefined) { body.style.backgroundColor = 'rgb(255, 255, 255)'; return; } body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)'; }

¡Esperaba que esto ayudara!

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