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 ACTUALITZACIONS VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… Inici / codificació / JavaScript i Ajax / JavaScript: Com obtenir el i ...

JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Com obtenir l'índex (posició dins d'un grup) d'un objecte amb jQuery?

Posted on 20. Publicat el 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Juny de 2009, per Dragos a la codificació, JavaScript i Ajax

I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. He passat algun temps avui esbrinar com obtenir aquest índex i, finalment acabo de perplexitat a terme.

Let's say we have a group of objects all containing the class=”house”. Diguem que tenim un grup de tots els objectes que conté la classe = "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: Ara bé, si està fent referència al primer element dins del grup i ens agradaria saber és l'índex (i és obvi que en aquest cas), hem d'utilitzar aquest fragment de codi:

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

So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Així, el codi funciona de la següent manera: estem buscant en tots els elements d'un grup de tots els agrupats per la classe = "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')) ). Ara estem buscant l'índex de si un element dins d'aquest grup, en el nostre cas és el primer element del grup ($ ( '. Casa'). Índex de ($('. casa: en primer lloc'))).

Here's another example to help you better understand how index() works: Heus aquí un altre exemple per ajudar a comprendre millor la forma d'índex () 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 »). índex (this)); / / alertes 3, suposant que hi ha 3 o més elements amb 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. Així que, bàsicament, si cal esbrinar l'índex d'un element dins d'un grup, primer ha d'especificar el grup i després passar l'objecte que necessita per trobar l'índex com un paràmetre a la funció index ().

Translate this post Demanar aquest missatge


Related posts: Llocs relacionats amb:

  1. JavaScript: GIFless animation. JavaScript: animació GIFless. Animate images,logos with jQuery Imatges animades, logos amb jQuery
  2. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Què passa si l'animació jQuery no es dispara / inici?
  3. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: On puc trobar totes les propietats de tots els elements HTML?
  4. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Enviar funcionar com un paràmetre a una altra funció (callbacks)
  5. JavaScript: Get anchor from URL JavaScript: Obtenir l'àncora de l'URL

    blog comments powered by Disqus blog alimentat per Disqus