Ver Mensaje Individual
  #8 (permalink)  
Antiguo 01/04/2011, 08:42
vanessacruz_7
 
Fecha de Ingreso: marzo-2011
Mensajes: 12
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: Problema If de boton 1 vez pulsado tenga 1 color y al volverlo a pulsar el

Cita:
Iniciado por quimfv Ver Mensaje
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 [URL="http://www.tek-tips.com/viewthread.cfm?qid=1436140&page=11"]aqui[/URL].

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



Hola, gracias por tu ayuda, me ha servido el código... el problema que tengo es que el boton al ser presionado no permance con el color cambiado, sino que vuelve a su estado original en menos de un segundo =( ... cuando se presiona un boton el ejecuta una acción(muestra una tabla)... quiero que quede en negro cuando se presione, hasta que se vuelva a presionar otro boton (son 7 botones, cada uno genera una tabla diferente) . Para generar las tablas tengo programado en php. por favor, me podrias ayudar? gracias