Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/03/2011, 22:39
Avatar de Panino5001
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: AddEventListener's funcionando como array

Lo que estás haciendo (que está mal) es esto:
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>
<style>
divwidth:300pxheight:25pxline-height:25pxborder:1px solid #000; font-size:14px; padding:3px; margin-bottom:2px}
</style>
</
head>

<
body>
<
div>uno</div>
<
div>dos</div>
<
div>tres</div>
<
script type="text/javascript">
function 
GM_setValue(j,t){
    
//la agregamos para test fuera de GM
}
function 
fillt(i,e){
    
e.preventDefault();
    if(
document.getElementsByTagName('div')[i])
        var 
juanito=document.getElementsByTagName('div')[i].innerHTML;
    else{
        
alert('el índice '+i+' es inválido');
        var 
juanito='Error';
    }
        
    
GM_setValue(juanito,true);
    
alert("you've just untracked "+juanito);
}
var 
allElements=document.getElementsByTagName('div'),length=allElements.length;
for(var 
i=0;i<length;i++){
    var 
=document.createElement('a');
    
a.href='#';
    
a.innerHTML=allElements[i].innerHTML;
    
a.addEventListener('click' , function(e){fillt(i,e);}, false);
    
    
allElements[i].parentNode.insertBefore(aallElements[i].nextSibling);

}
</script>

</body>
</html> 
Lo que deberías hacer (por la razón que explica el enlace que te indiqué en mi post anterior) es esto:
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>
<style>
divwidth:300pxheight:25pxline-height:25pxborder:1px solid #000; font-size:14px; padding:3px; margin-bottom:2px}
</style>
</
head>

<
body>
<
div>uno</div>
<
div>dos</div>
<
div>tres</div>
<
script type="text/javascript">
function 
GM_setValue(j,t){
    
//la agregamos para test fuera de GM
}
function 
fillt(i,e){
    
e.preventDefault();
    if(
document.getElementsByTagName('div')[i])
        var 
juanito=document.getElementsByTagName('div')[i].innerHTML;
    else{
        
alert('el índice '+i+' es inválido');
        var 
juanito='Error';
    }
        
    
GM_setValue(juanito,true);
    
alert("you've just untracked "+juanito);
}
var 
allElements=document.getElementsByTagName('div'),length=allElements.length;
for(var 
i=0;i<length;i++){
    var 
=document.createElement('a');
    
a.href='#';
    
a.innerHTML=allElements[i].innerHTML;
    
//a.addEventListener('click' , function(e){fillt(i,e);}, false);
    
(function(i){a.addEventListener('click' , function(e){fillt(i,e);}, false);})(i);
    
allElements[i].parentNode.insertBefore(aallElements[i].nextSibling);

}
</script>

</body>
</html> 
En este último código aparece comentada la línea errónea y la correcta debajo

Última edición por Panino5001; 10/03/2011 a las 22:44