This is a translated page. The original can be found here: http://iwebdevel.com/2009/06/20/javascript-how-to-get-the-index-position-within-a-group-of-an-object-with-jquery/
UPDATES VIA RSS | Email ACTUALIZACIONES VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… Inicio / codificación / JavaScript y Ajax / JavaScript: Cómo obtener el i ...

JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Cómo obtener el índice (posición dentro de un grupo) de un objeto con jQuery?

Posted on 20. Publicado el 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Junio de 2009, por Dragos en la codificación, JavaScript y Ajax

I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. He pasado algún tiempo hoy averiguar cómo obtener ese índice y, finalmente acabo de perplejidad a cabo.

Let's say we have a group of objects all containing the class=”house”. Digamos que tenemos un grupo de todos los objetos que contiene la clase = "casa". Now if we are referencing to the first element within the group and we'd like to find out it's index (well it's obvious in this case), we'd use this piece of code: Ahora bien, si está haciendo referencia al primer elemento dentro del grupo y nos gustaría saber es el índice (y es obvio que en este caso), tenemos que utilizar este fragmento de código:

 var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; indexOfFirstElement var =$('. casa '). índice ($('. casa: en primer lugar')); / / indexOfFirstElement = 0; 

So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Así, el código funciona de la siguiente manera: estamos buscando en todos los elementos de un grupo de todos los agrupados por la clase = "casa" ($ ( '. Casa')). Now we are looking for the index if an element within this group, in our case it's the first element of the group ( $('.house') .index($('.house:first')) ). Ahora estamos buscando el índice de si un elemento dentro de este grupo, en nuestro caso es el primer elemento del grupo ($ ( '. Casa'). Índice de ($('. casa: en primer lugar '))).

Here's another example to help you better understand how index() works: He aquí otro ejemplo para ayudar a comprender mejor la forma de índice () funciona:

 $('.house').each(function(i){ $ ( '. casa'). each (function (i) ( 
      if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" if (i == 3) casa ($('. alerta »). índice (this)); / / alertas 3, suponiendo que hay 3 o más elementos con class =" casa " 
 }); )); 

So basically, if you need to find out the index of an element within a group, you should first specify the group and then pass the object you need to find the index of as a parameter to the index() function. Así que, básicamente, si es necesario averiguar el índice de un elemento dentro de un grupo, primero debe especificar el grupo y luego pasar el objeto que necesita para encontrar el índice como un parámetro a la función index ().

Translate this post Traducir este mensaje


Related posts: Puestos relacionados con:

  1. JavaScript: GIFless animation. JavaScript: animación GIFless. Animate images,logos with jQuery Imágenes animadas, logos con jQuery
  2. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Enviar funcionar como un parámetro a otra función (callbacks)
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: ¿Qué pasa si la animación jQuery no se dispara / inicio?
  4. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: ¿Dónde puedo encontrar todas las propiedades de todos los elementos HTML?
  5. Javascript: How to validate email address with JavaScript? Javascript: Cómo validar la dirección de correo electrónico con JavaScript?

    blog comments powered by Disqus blog alimentado por Disqus