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

134
Visualizações
El error de tipo no detectado de Javascript no puede leer la propiedad de nulo. ¿El error está siendo causado por una página HTML diferente?

Así que estoy trabajando en un sitio y decidí usar 5 páginas html diferentes, algo que realmente no hago. Estoy usando un archivo javascript, y creo que cuando voy a contact.html JS no puedo encontrar los elementos que están en index.html, lo cual tiene sentido. pero como soluciono esto?

guión.js

 //code i'd like to run in index.html let highlighter = (selected, ignored1, ignored2) => { document.getElementById(selected).addEventListener("click", ()=> { let highlighted = document.getElementById(selected).style.backgroundColor = "#ECECEC"; let ignored = [ignored1,ignored2]; for (let i = 0; i < ignored.length; i++) { document.getElementById(ignored[i]).style.backgroundColor = "#FFFFFF" } }); } highlighter("alpha", "beta", "charlie"); highlighter("beta", "alpha", "charlie"); highlighter("charlie", "alpha", "beta"); //code i'd like to run in contact.html document.getElementById("contact-us").addEventListener("click", ()=> { alert("contact us test"); }); //code i'd like to have run everywhere alert('test');

índice.html

 <html> <body> <h1 id="alpha">test</h1> <h1 id="beta">test</h1> <h1 id="charlie">test</h1> </body> </html>

contacto.html

 <html> <body> <div id="contact-us"> <h1>Click me!</h1> </div> </body> </html>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Primero debe verificar si el elemento existe o no con condicional:

 let highlighter = (selected, ignored1, ignored2) => { if(!document.getElementById(selected)) { return; } document.getElementById(selected).addEventListener("click", ()=> { let highlighted = document.getElementById(selected).style.backgroundColor = "#ECECEC"; let ignored = [ignored1,ignored2]; for (let i = 0; i < ignored.length; i++) { document.getElementById(ignored[i]).style.backgroundColor = "#FFFFFF" } }); } highlighter("alpha", "beta", "charlie"); highlighter("beta", "alpha", "charlie"); highlighter("charlie", "alpha", "beta"); //code i'd like to run in contact.html if(document.getElementById("contact-us") { document.getElementById("contact-us").addEventListener("click", ()=> { alert("contact us test"); }); } //code i'd like to have run everywhere alert('test');
about 4 years ago · Santiago Trujillo 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