Gallery

martes, 20 de octubre de 2026

JS - Cambia la hoja de estilos CSS

Cambiar la hoja de estilo con Javascript al hacer clic




El código esta demasiado bueno como para no tenerlo en el sitio.
Recuerda solo modificar lo que vayas a necesitar.

HTML (index.html)
html
<head>
  <link id="estilo" rel="stylesheet" href="estilo1.css">
</head>
<body>
  <button id="miBoton">Cambiar Estilo</button>
</body>

Javascript

const boton = document.getElementById('miBoton');
const hojaEstilo = document.getElementById('estilo');

boton.addEventListener('click', () => {
  if (hojaEstilo.getAttribute('href') === 'estilo1.css') {
    hojaEstilo.setAttribute('href', 'estilo2.css');
  } else {
    hojaEstilo.setAttribute('href', 'estilo1.css');
  }
});

No hay comentarios:

Publicar un comentario

Documental de Hackers