Ver Mensaje Individual
  #8 (permalink)  
Antiguo 19/04/2012, 15:40
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: agregar y quitar archivos estilos css

Finalmente me quedó asi
todo en una función
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>Cambiar Estilos</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript">
  7. //<![CDATA[
  8.  
  9. function cambiaEstilos(nombre,accion){
  10.  
  11. if(accion == 'a'){ // agregar estilos
  12. var elCss  = document.createElement('link');
  13. elCss.rel = 'stylesheet'
  14. elCss.type = 'text/css';
  15. elCss.href = nombre + '.css';
  16. document.getElementsByTagName('head')[0].appendChild(elCss);
  17. }else if(accion == 'r'){ //remover estilos
  18. var losEstilos=document.getElementsByTagName('link')
  19. for (var i=losEstilos.length; i>=0; i--){
  20. if (losEstilos[i] && losEstilos[i].getAttribute('href')!=null && losEstilos[i].getAttribute('href').indexOf(nombre+'.css')!=-1)
  21. losEstilos[i].parentNode.removeChild(losEstilos[i])
  22. }
  23. }
  24.  
  25. }
  26.  
  27. //]]>
  28. </head>
  29. <h1>Hola</h1>
  30. <!-- parámetros para la función, nombre del archivo css sin extensión, a para agregar, r para remover -->
  31. <button onclick="cambiaEstilos('original','a');">Agregar estilo original.css</button>
  32. <button onclick="cambiaEstilos('original','r');">Eliminar estilo original.css</button>
  33. <button onclick="cambiaEstilos('verde','a');">Agregar estilo verde.css</button>
  34. <button onclick="cambiaEstilos('verde','r');">Eliminar estilo verde.css</button>
  35. </body>
  36. </html>

Testeado en FF / IE9-8-7 / Opera /Chrome
Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.