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

113
Views
¿Cómo puedo agregar una etiqueta de 'enlace' en 'cabeza' usando JavaScript?

Quiero crear una función para mi sitio web donde el usuario pueda seleccionar la forma en que se mostrará el estilo a través de un formulario.

Enlacé los elementos del formulario a un archivo JS, pero no funciona en absoluto.

Aquí está el código HTML:

 <form> <label for="theme-selector">Select your theme:</label> <select name="theme-selector" id="theme-selector"> <option value="auto">Automatic (default)</option> <option value="light">Light</option> <option value="dark">Dark</option> </select> <button id="apply-theme">Apply</button> </form>

Así es como lo vinculé al script:

 let theme = document.getElementById('theme-selector').value; let applyBtn = document.getElementById('apply-theme'); let time = new Date(); time = time.getHours(); applyBtn.addEventListener('click', () => { let node = document.createElement('link'); node.rel = 'stylesheet'; if(theme == 'auto') { if(time >= 10 && time <= 19) { // daytime theme node.href = 'style/light.css'; } else { // nighttime theme node.href = 'style/dark.css'; } } else { // chosen theme node.href = `style/${theme}.css`; } document.head.appendChild(node); });

La cosa "automática" está hecha para establecer diferentes temas dependiendo de la hora que sea.

No entiendo por qué no funciona. Soy muy nuevo en JavaScript, así que por favor sea amable, ¡gracias!

about 4 years ago · Juan Pablo Isaza
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!