Ver Mensaje Individual
  #2 (permalink)  
Antiguo 11/11/2010, 06:18
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

1. El nombre del atributo es backgroundColor no background.
2. Si haces un

Código Javascript:
Ver original
  1. alert(document.getElementById(COLOR.id).style.backgroundColor)

veras que en vez de decir #000000 dice rgb(0, 0, 0).

Y ahora alucina con la solución que te he encontrado

Código Javascript:
Ver original
  1. function Funcion2(COLOR) {
  2.     var aux=document.getElementById(COLOR.id).style.backgroundColor;
  3.     alert(aux);
  4.         if (rgbConvert(aux) == "#000000"){
  5.                 document.getElementById(COLOR.id).style.backgroundColor="#ffffff";     
  6.         }
  7.         else {
  8.                 document.getElementById(COLOR.id).style.backgroundColor = "#000000";
  9.         }  
  10.     }
  11. function rgbConvert(str) {
  12.    str = str.replace(/rgb\(|\)/g, "").split(",");
  13.    str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
  14.    str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
  15.    str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
  16.    str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
  17.    str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
  18.    str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
  19.    return ('#' + str.join(""));
  20. }


Código HTML:
Ver original
  1. <input name="boton" type="button" id="boton" onClick="Funcion2(this)" style="background-color: #000000">

Una funcion que convierte el texto rgb(0,0,0) a #000000

Fijate que uso this para pasar el objeto, esto lo puedes cambiar y poner directamente el id del boton.

Tambien puedes sacar el alert().

Lo he encontrado aqui.

Si alguien lo sabe, nos podría explicar el porque de esa, aparentemente, incoherencia de javascript.


Claro que como se trata de textos tambien se puede hacer esto y funciona

Código Javascript:
Ver original
  1. function Funcion2(COLOR) {
  2.     var aux=document.getElementById(COLOR.id).style.backgroundColor;
  3.     alert(aux);
  4.         if (aux == "rgb(0, 0, 0)"){
  5.                 document.getElementById(COLOR.id).style.backgroundColor="#ffffff";     
  6.         }
  7.         else {
  8.                 document.getElementById(COLOR.id).style.backgroundColor = "#000000";
  9.         }  
  10.     }

Fijate en las comillas aux == "rgb(0, 0, 0)"

Quim