Ver Mensaje Individual
  #4 (permalink)  
Antiguo 25/07/2009, 09:24
Avatar de Legoltaz
Legoltaz
 
Fecha de Ingreso: agosto-2008
Mensajes: 325
Antigüedad: 15 años, 9 meses
Puntos: 6
Respuesta: jQuery: Problemas con uso de variable

Cita:
Iniciado por eall Ver Mensaje
el problema es que lo que tu quieres hacer está fuera del each.

Código javascript:
Ver original
  1. $(document).ready(function(){
  2.     imgs = document.getElementsByTagName("img");
  3.     $.each(imgs,function(i,n){
  4.         w = n.width; // w queda con el último valor.
  5.     });
  6.     $("img").css({width:80,height:80});
  7.     $("img").click(function(){
  8.         alert(w);
  9.     });
  10. });
  11.  
  12. $('img').click(function(){
  13.    var h = $(this).attr('width');
  14.    var w = $(this).attr('height');
  15.    $(this).animate({width:w,height:h},"slow");
  16. });

espero te ayude con tu problema...
Gracias por la respuesta, pero aún no está bien. Ahora al hacer click no hace nada, porque creo que se le está asignando de nuevo 80px de width height con h y w.

Estaba probando con esto, pero solo me funciona con la última imagen (le asigna a todas las imagenes los valores width y height de la ultima):

Código JavaScript:
Ver original
  1. $(document).ready(function(){
  2.     $("img").each(function(){
  3.         w = $(this).width();
  4.         h = $(this).height();
  5.     });
  6.     $("img").css({width:80,height:80});
  7.     $("img").click(function(){
  8.         $(this).animate({width:w,height:h},"slow");
  9.     });
  10. });