Foros del Web » Programando para Internet » Javascript »

Elemento this en attachEvent

Estas en el tema de Elemento this en attachEvent en el foro de Javascript en Foros del Web. Hola, estoy usando ahora esta función para agregar eventos. @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Javascript : Ver original function addBind ( obj , e , fn ) ...
  #1 (permalink)  
Antiguo 02/05/2010, 00:47
Avatar de America|UNK  
Fecha de Ingreso: noviembre-2006
Ubicación: Piura - Perú
Mensajes: 582
Antigüedad: 17 años, 4 meses
Puntos: 56
Elemento this en attachEvent

Hola, estoy usando ahora esta función para agregar eventos.

Código Javascript:
Ver original
  1. function addBind(obj,e,fn){
  2.         if (window.attachEvent) {
  3.             obj.attachEvent("on" + e, fn); // IE
  4.         }else{
  5.             obj.addEventListener(e, fn, false); // FF
  6.         }
  7.     }
  8.  
  9. // veamos si es verdad
  10.    
  11.     document.write('<div class="clasOk" id="test">Hola mundo</div>')
  12.        
  13.     addBind(document.getElementById("test"),"click",function(){
  14.         alert(this.className); // IE: undefined, FF: classOk
  15.     })

EL problema es que cuando arranco el evento click en IE, el objeto this no me devuelve el objeto actual, el className me da undefined, en firefox me devuelve classOk, que es el nombre de la clase.

Habrá manera de obtener el elemento this con attachEvent ?, gracias y Saludos.
__________________
/* El que atiende, entiende..., el que entiende, aprende!.
Desarrollo Web Freelance, Contactar */
  #2 (permalink)  
Antiguo 02/05/2010, 01:41
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 19 años, 11 meses
Puntos: 834
Respuesta: Elemento this en attachEvent

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></title>
</
head>

<
body>
<
script>
function 
addBind(obj,e,fn){
    if (
window.attachEvent) {
        var 
f=function(){fn.call(obj,window.event);};
        
obj.attachEvent("on" ef); // IE
    
}else{
        
obj.addEventListener(efnfalse); // FF
    
}
}
// veamos si es verdad
document.write('<div class="clasOk" id="test">Hola mundo</div>')
addBind(document.getElementById("test"),"click",function(){
alert(this.className); // IE: classOk, FF: classOk
})
</script>
</body>
</html> 
Te recomiendo leas esto: http://www.dustindiaz.com/javascript-no-no/

Última edición por Panino5001; 02/05/2010 a las 01:47
  #3 (permalink)  
Antiguo 02/05/2010, 10:53
Avatar de America|UNK  
Fecha de Ingreso: noviembre-2006
Ubicación: Piura - Perú
Mensajes: 582
Antigüedad: 17 años, 4 meses
Puntos: 56
Respuesta: Elemento this en attachEvent

Me ha servido, nunca se me hubiera ocurrido en serio, grande panino!
__________________
/* El que atiende, entiende..., el que entiende, aprende!.
Desarrollo Web Freelance, Contactar */

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 06:03.