Ver Mensaje Individual
  #5 (permalink)  
Antiguo 12/11/2010, 02:20
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 2 meses
Puntos: 574
Respuesta: Problema If de boton 1 vez pulsado tenga 1 color y al volverlo a pulsar el

Pues Tecna tiene razon y por tanto la solucion no sierve para todos los navegadores.

Una vez mas tendremos que programar dos versiones de lo mismo, en este caso es fàcil

Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <title>Documento sin t&iacute;tulo</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. </head>
  5. <script language="JavaScript" type="text/JavaScript">
  6. function Funcion2(COLOR) {
  7.     var aux=document.getElementById(COLOR.id).style.backgroundColor;//innecesario
  8.     alert(aux);//innecesario
  9.     if(document.getElementById(COLOR.id).style.backgroundColor.indexOf("#")!=-1){
  10.             aux=document.getElementById(COLOR.id).style.backgroundColor;
  11.         }else{
  12.             aux=rgbConvert(document.getElementById(COLOR.id).style.backgroundColor);
  13.         }
  14.         if (aux == "#000000"){
  15.                 document.getElementById(COLOR.id).style.backgroundColor="#ffffff";      
  16.         }
  17.         else {
  18.                 document.getElementById(COLOR.id).style.backgroundColor = "#000000";
  19.         }  
  20.     }
  21. function rgbConvert(str) {
  22.    str = str.replace(/rgb\(|\)/g, "").split(",");
  23.    str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
  24.    str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
  25.    str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
  26.    str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
  27.    str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
  28.    str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
  29.    return ('#' + str.join(""));
  30. }</script>
  31.  
  32. <input name="boton" type="button" id="boton" onClick="Funcion2(this)" style="background-color: #000000">
  33. </body>
  34. </html>

Quizas una forma mas elegante seria definir dos clases CSS (digamos botonblanco, botonnegro) usar .className en la función


Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <title>Documento sin t&iacute;tulo</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. </head>
  5. <script language="JavaScript" type="text/JavaScript">
  6. function Funcion2(COLOR) {
  7.     var aux=document.getElementById(COLOR.id).className;//innecesario
  8.     alert(aux);//innecesario
  9.         if (document.getElementById(COLOR.id).className == "botonBlanco"){
  10.                 document.getElementById(COLOR.id).className = "botonNegro";      
  11.         }
  12.         else {
  13.                 document.getElementById(COLOR.id).className = "botonBlanco";
  14.         }  
  15.     }
  16. .botonBlanco{
  17.         background-color: #FFFFFF;
  18. }
  19.  
  20. .botonNegro{
  21.          background-color: #000000;
  22. }
  23.  
  24. <input name="boton" type="button" id="boton" onClick="Funcion2(this)" class="botonBlanco">
  25. </body>
  26. </html>


Elige la que mas te guste, estas funcionan en FF y IE.

Quim