Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Obtener posicion

Estas en el tema de Obtener posicion en el foro de Frameworks JS en Foros del Web. En el sgte. código tengo una lista con 3 Items, si hago click en el segundo Item, como puedo saber que hice click en el ...
  #1 (permalink)  
Antiguo 15/10/2010, 17:28
Avatar de chicohot20  
Fecha de Ingreso: mayo-2009
Mensajes: 388
Antigüedad: 15 años
Puntos: 43
Obtener posicion

En el sgte. código tengo una lista con 3 Items, si hago click en el segundo Item, como puedo saber que hice click en el 2do li?

Código HTML:
Ver original
  1. $("li").click(function(){
  2.    //Este li en que posicion esta?
  3. });
  4.  
  5. <ul>
  6.  <li>Item 1</li>
  7.  <li>Item 2</li>
  8.  <li>Item 3</li>
  9. </ul>
  #2 (permalink)  
Antiguo 15/10/2010, 18:20
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: Obtener posicion

Sin Framework sería así (quizá te sirva de guía):
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ítulo</title>
<
script type="text/javascript">
function $(
x){return document.getElementById(x);}
function 
addEvent(obj,fun,type){ 
    if(
obj.addEventListener){ 
        
obj.addEventListener(type,fun,false); 
    }else if(
obj.attachEvent){ 
        var 
f=function(){ 
            
fun.call(obj,window.event); 
        } 
        
obj.attachEvent('on'+type,f); 
        
obj[fun.toString()+type]=f
    }else{ 
        
obj['on'+type]=fun
    } 
}  
onload=function(){
    var 
li=document.getElementsByTagName('li');
    for(var 
i=0,l=li.length;i<l;i++){
        
addEvent(li[i],function(){
            var 
p=this,c=0;
            while(
&& p.nodeName.toLowerCase()!='ul'){
                if(
p.nodeName.toLowerCase()=='li'){
                    
c++;    
                }
                
p=p.previousSibling;
            }
            
alert(c);
        },
'click');
    }
}

</script>

</head>

<body>
<ul>
 <li>Item 1</li>
 <li>Item 2</li>
 <li>Item 3</li>
</ul>

</body>
</html> 
  #3 (permalink)  
Antiguo 15/10/2010, 20:03
Avatar de chicohot20  
Fecha de Ingreso: mayo-2009
Mensajes: 388
Antigüedad: 15 años
Puntos: 43
Respuesta: Obtener posicion

Gracias por la solucion, funciona bien, pero tambien hice mi propia solución en Jquery, pongo el codigo para los que quieran.
Código Javascript:
Ver original
  1. $("li").click(function(){
  2.         var current = $(this);
  3.         var abort = false;
  4.         var i=0;
  5.         while (!abort) {
  6.             if (current.prev().length!=0){     
  7.                 i++;
  8.                 current=current.prev();
  9.             }
  10.             else abort = true;
  11.         }
  12.         alert(i)
  13.         return false;
  14.     });
  15. <ul>
  16.  <li>Item 1</li>
  17.  <li>Item 2</li>
  18.  <li>Item 3</li>
  19. </ul>
  #4 (permalink)  
Antiguo 16/10/2010, 12:28
 
Fecha de Ingreso: diciembre-2009
Ubicación: Misiones
Mensajes: 867
Antigüedad: 14 años, 5 meses
Puntos: 65
Respuesta: Obtener posicion

jQuery tiene un método para obtener el índice de elementos, fijate la doc http://api.jquery.com/index/

las posiciones comienzan con 0
Código HTML:
Ver original
  1.     <title>Pruebas</title>
  2. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  3. $(function(){
  4.     $("li").click(function(){
  5.         alert ( $(this).index() );
  6.     });
  7. });
  8.  
  9. </head>
  10. <ul>
  11.    <li>Item 1</li>
  12.    <li>Item 2</li>
  13.    <li>Item 3</li>
  14. </ul>
  15. </body>
  16. </html>
  #5 (permalink)  
Antiguo 16/10/2010, 14:00
Avatar de chicohot20  
Fecha de Ingreso: mayo-2009
Mensajes: 388
Antigüedad: 15 años
Puntos: 43
Respuesta: Obtener posicion

No me funciona el codigo Dany_s me devuelve -1 porque?
  #6 (permalink)  
Antiguo 16/10/2010, 14:05
Avatar de chicohot20  
Fecha de Ingreso: mayo-2009
Mensajes: 388
Antigüedad: 15 años
Puntos: 43
Respuesta: Obtener posicion

El error es mío, la versión de Jquery debe ser 1.4, debo actualizarme no sabía.

Gracias Dany_s,

Etiquetas: posicion
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 17:47.