JQuery - problema con bucle for jquery

 
Vista:

problema con bucle for jquery

Publicado por Nestor (1 intervención) el 22/04/2013 00:53:30
Hola a todos, tengan un buen dia.
bueno a ver si me pueden ayudarme a resolver este problema que tengo con ese codigo.

cuando hago click en imagen0 me muestra un mensaje de diciendo este es la IMAGEN 5.
y no deberia mostrarme asi si no que cuando haga click en cada imagen me muestre un mensaje correspondiente a cada imagen.
ejemplo
click imagen 0, mensaje este es la imagen0
click imagen 1, mensaje este es la imagen1
click imagen 2, mensaje este es la imagen2

asi sucesivamente.

Ayudenme por favor a solucionar el codigo o como podria ser, algo mal estoy haciendo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
for(var i=0;i<5;i++){
$("#"+"imagen"+i).click(function(event) {
 
alert("este es la imagen"+i);
});
 
}
});
</script>
</head>
<body>
<table>
<tr>
<td><div id="imagen0"><img src="../uploads/00001.JPG" width="40px" height="40px"/></div></td>
<td><div id="imagen1"><img src="../uploads/00002.JPG" width="40px" height="40px"/></div></td>
<td><div id="imagen2"><img src="../uploads/00003.JPG" width="40px" height="40px"/></div></td>
<td><div id="imagen3"><img src="../uploads/00004.JPG" width="40px" height="40px"/></div></td>
<td><div id="imagen4"><img src="../uploads/00005.JPG" width="40px" height="40px"/></div></td>
</tr>
</table>
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

problema con bucle for jquery

Publicado por fede (5 intervenciones) el 29/04/2013 16:45:32
yo lo haria mas simple, ej:

1
2
3
4
5
6
$(table tr td img).click(function(){
    var _this = $(this);
    var _padre = $(_this).parent('div');
    alert("esta es la imagen "+_padre.attr('id'));
 
})
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar