Foros del Web » Programando para Internet » Javascript »

problema con attachEvent y parametro (this)

Estas en el tema de problema con attachEvent y parametro (this) en el foro de Javascript en Foros del Web. Tengo un objeto creado dinamicamente al cual le agrego el evento "click" que llama a una función x hasta acá todo bien, el problema viene ...
  #1 (permalink)  
Antiguo 05/03/2011, 10:10
Avatar de asdmaster  
Fecha de Ingreso: marzo-2010
Mensajes: 98
Antigüedad: 14 años, 1 mes
Puntos: 2
problema con attachEvent y parametro (this)

Tengo un objeto creado dinamicamente al cual le agrego el evento "click" que llama a una función x hasta acá todo bien, el problema viene con IE cuando quiero pasar el parámetro this que hace referencia al mismo objeto, en vez de pasar un objeto de tipo [object HTMLImageElement] pasa este [object windows] .

Alguna idea de que estoy haciendo mal.

Javascript

Código Javascript:
Ver original
  1. var img = document.createElement('img');
  2. // Agregar evento onclick
  3. if (img.addEventListener) img.addEventListener("click", function(){editorEmoticon(this);}, false);
  4. else if (img.attachEvent) img.attachEvent("onclick", function(){editorEmoticon(this);});

He intentado pasar el objeto img como parámetro directamente en vez la variable this a la función y funciona pero el problema es que esto se hace dentro de un bucle y de esta manera me toma el valor del ultimo objeto creado por lo que se me imagina que tal vez sea algún problema de referencia o algo así.

Gracias...
  #2 (permalink)  
Antiguo 05/03/2011, 10:26
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: problema con attachEvent y parametro (this)

Probá así:
Código PHP:
<!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&#237;tulo</title>

</head>

<
body>
<
script type="text/javascript">
function 
editorEmoticon(yo){
    
alert(yo.src);
}
 var 
img document.createElement('img');
 
img.src='img2/1.jpg';
// Agregar evento onclick
(function(img){
    if (
img.addEventListenerimg.addEventListener("click", function(){editorEmoticon(this);}, false);
    else if (
img.attachEvent) {img.attachEvent("onclick", function(){editorEmoticon.call(img,img);})};
})(
img);

document.body.appendChild(img);
</script>
</body>
</html> 
O mejor:
Código PHP:
<!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&#237;tulo</title>

</head>

<
body>
<
script type="text/javascript">
function 
editorEmoticon(){
    
alert(this.src);
}
 var 
img document.createElement('img');
 
img.src='img2/1.jpg';
// Agregar evento onclick
(function(img){
    if (
img.addEventListenerimg.addEventListener("click"editorEmoticonfalse);
    else if (
img.attachEvent) {img.attachEvent("onclick", function(){editorEmoticon.call(img);})};
})(
img);

document.body.appendChild(img);
</script>
</body>
</html> 
  #3 (permalink)  
Antiguo 05/03/2011, 10:38
Avatar de asdmaster  
Fecha de Ingreso: marzo-2010
Mensajes: 98
Antigüedad: 14 años, 1 mes
Puntos: 2
Respuesta: problema con attachEvent y parametro (this)

Gracias Panino5001 funciona perfecto... ahora analizare el código para entenderlo y asimilarlo...

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:37.