Ver Mensaje Individual
  #5 (permalink)  
Antiguo 15/02/2011, 16:19
omarMusic
 
Fecha de Ingreso: febrero-2011
Ubicación: Evolandia
Mensajes: 103
Antigüedad: 13 años, 2 meses
Puntos: 10
Respuesta: Objetos javascript

Holas, el problema es que los objetos tienen que estar declarados para poder acceder a ellos, por ej:

Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2.     <head>
  3.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4.         <title>Objetos JS</title>
  5.         <script type="text/javascript" src="jquery.js"></script>
  6.         <script type="text/javascript">
  7.             var objeto;
  8.            
  9.             function Gato (color, edad) {
  10.                 this.color = color;
  11.                 this.edad = edad;
  12.                 this.mostrar = mostrar;
  13.                 this.cambiare = cambiare;
  14.             }
  15.  
  16.             function crear() {
  17.                 var color = "Negro";
  18.                 var edad = 10;
  19.                 objeto = new Gato(color, edad)
  20.                 objeto.mostrar();
  21.                 objeto.cambiare();
  22.             }
  23.            
  24.             function mostrar(){
  25.                 var $p = $('<p>');
  26.                 var id = "textogato";
  27.                 var $objeto = $p.attr("id", id);
  28.                 var $texto = this.color;
  29.                 var $objeto = $p.text($texto);  
  30.                 $objeto.css("display", "inline");
  31.                 $objeto.css("background", "red");
  32.                 $objeto.css("font-size", "20px");
  33.                 $objeto.css("color", "blue");
  34.                 $objeto.appendTo('body');
  35.             }
  36.  
  37.             function cambiare () {
  38.                 this.edad++;
  39.                 alert("Nueva edad: " + this.edad)
  40.             }
  41.            
  42.             function aumentarEdad () {
  43.                 objeto.cambiare();
  44.             }
  45.          </script>
  46.     </head>
  47.     <body>
  48.         <input type="button" value="Crear objeto" onclick="crear();">
  49.         <input type="button" value="Aumentar edad" id="edad" onclick="aumentarEdad();">
  50.     </body>
  51. </html>

si alguien sabe como declarar objetos dinámicamente y acceder a ellos, q lo diga, necesito saberlo también