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 Actualizacións Via RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… Home / Codificación / JavaScript e Ajax / JavaScript: Como obter o i ...

JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Como obter o índice (posición dentro dun grupo) dun obxecto con jQuery?

Posted on 20. Posta en 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Xuño de 2009 por Dragos na Codificación, JavaScript e Ajax

I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Eu pase algún tempo hoxe para descubrir como obter este índice e, finalmente, acaba intrigado para fóra.

Let's say we have a group of objects all containing the class=”house”. Digamos que temos un grupo de todos os obxectos que contén o class = "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: Agora, se está facendo referencia ó primeiro elemento dentro do grupo e nós queremos descubrir que o índice (ben, é obvio, neste caso), usaríamos este anaco de código:

 var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; indexOfFirstElement var =$('. casa '). index casa ($('.: first')); / / 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í, o código funciona do seguinte xeito: nós estamos buscando dentro de todos os elementos dun grupo todos agrupados polo class = "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')) ). Agora nós estamos ollando para o índice se un elemento dentro deste grupo, no noso caso é o primeiro elemento do grupo ($ ( 'casa'.). Index casa ($('.: first'))).

Here's another example to help you better understand how index() works: Velaí outro exemplo para axudar a entender mellor como í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 '). index (this)); / 3 indicacións, supoñendo que hai 3 ou máis elementos 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. Entón, basicamente, se cómpre descubrir o contido dun elemento dentro dun grupo, primeiro ten que especificar o grupo e, en seguida, pasar o obxecto que precisa para atopar o contido como un parámetro para o índice () function.

Translate this post Traducir esta post


Related posts: Related posts:

  1. JavaScript: GIFless animation. JavaScript: animación GIFless. Animate images,logos with jQuery Ánimo fotos, logotipos con jQuery
  2. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: E se a animación jQuery non activar / comezar?
  3. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Enviar funcionar como un parámetro para outra función (callbacks)
  4. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Onde podo atopar todas as propiedades para todos os elementos HTML?
  5. JavaScript: Get anchor from URL JavaScript: Get Referencia de URL

    blog comments powered by Disqus blog comments powered by Disqus