Ver Mensaje Individual
  #12 (permalink)  
Antiguo 20/04/2012, 19:25
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

Bueno, probé la ultima script de @Melecio, y funciona bien, de todas maneras al hacer la alternancia desde un mismo botón, creo que se le pueden hacer unas mejoras, además le hice un agregado extra, un botón que remueva todos los estilos, optimizaciones, bienvenidas


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. <style type="text/css">
  7. /*<![CDATA[*/
  8.  
  9. div.test{
  10. height: 120px;
  11. -webkit-box-sizing: border-box;
  12. -moz-box-sizing: border-box;  
  13. box-sizing: border-box;
  14. padding:0;
  15. margin: 30px;
  16. margin-bottom: 5px;
  17. }
  18.  
  19. h1{
  20. height: 60px;
  21. padding: 0;
  22. margin: 0;
  23. }
  24. div.botones{
  25. padding:0;
  26. margin: 30px;
  27. }
  28.  
  29. button{
  30. width: 250px;
  31. background-color:#9C8D7A;
  32. margin-bottom: 5px;
  33. outline: 0;
  34. }
  35. /*]]>*/
  36. <script type="text/javascript">
  37. //<![CDATA[
  38. var todos = new Array();
  39. function cambiaEstilos(nombre){
  40. var losEstilos=document.getElementsByTagName('link');
  41. if(todos.indexOf(nombre) == -1){
  42. var elCss  = document.createElement('link');
  43. elCss.rel = 'stylesheet'
  44. elCss.type = 'text/css';
  45. elCss.href = nombre + '.css';
  46. document.getElementsByTagName('head')[0].appendChild(elCss);
  47. todos.push(nombre);
  48. document.getElementById(nombre).innerHTML = "Eliminar estilo " + nombre + ".css";
  49. }else{
  50. todos.splice(todos.indexOf(nombre),1);
  51. document.getElementById(nombre).innerHTML = "Agregar estilo " + nombre + ".css";
  52. borrar(nombre);
  53. }
  54. }
  55.  
  56. function borrar(nombre){
  57. var losEstilos=document.getElementsByTagName('link');
  58. for (var i=losEstilos.length; i>=0; i--){
  59. if (losEstilos[i] && losEstilos[i].getAttribute('href')!=null && losEstilos[i].getAttribute('href').indexOf(nombre+'.css')!=-1){
  60. losEstilos[i].parentNode.removeChild(losEstilos[i]);
  61. }
  62. }
  63. }
  64.  
  65. function borraTodos(){
  66. for (var i=0; i < todos.length; i++ ){
  67. var todos_estilos = todos[i];
  68. borrar(todos_estilos);
  69. document.getElementById(todos_estilos).innerHTML = "Agregar estilo " + todos_estilos + ".css";
  70. }
  71. todos = new Array();
  72. }
  73.  
  74. //]]>
  75. </head>
  76. <div class="test">
  77. <h1>Hola</h1>
  78. </div>
  79. <div class="botones">
  80. <!-- parámetros para la función, nombre del archivo css sin extensión -->
  81. <button id="original" onclick="cambiaEstilos('original');">Agregar estilo original.css</button><br />
  82. <button id="background" onclick="cambiaEstilos('background');">Agregar estilo background.css</button><br />
  83. <button id="verde" onclick="cambiaEstilos('verde');">Agregar estilo verde.css</button><br /><br />
  84. <button onclick="borraTodos();">Eliminar todos los estilos</button>
  85. </div>
  86. </body>
  87. </html>

Por si acaso algún distraido, hay que crear los tres archivos de estilos (original.css, verde.css, background.css

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.