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 MISES A JOUR VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… Accueil / Codage / JavaScript et Ajax / JavaScript: Comment faire pour obtenir le i ...

JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Comment obtenir l'index (la position dans un groupe) d'un objet avec jQuery?

Posted on 20. Posté le 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Jun, 2009 par Dragos en codage, JavaScript et Ajax

I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. J'ai passé quelques temps aujourd'hui à trouver comment obtenir cet indice et, enfin, je viens de l'perplexe Out.

Let's say we have a group of objects all containing the class=”house”. Disons que nous avons un groupe d'objets contenant tous les class = "maison". 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: Maintenant, si nous le référencement sur le premier élément dans le groupe et nous aimerions connaître son index (bien que c'est évident, dans ce cas), nous aurions utilisé ce morceau de code:

 var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. maison »). Indice ($('. maison: tout d'abord ')); / / indexOfFirstElement = 0; 

So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Ainsi, le code fonctionne comme suit: Nous recherchons au sein de tous les éléments d'un groupe, toutes regroupées en fonction des class = "maison" ($ ( '. Maison')). 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')) ). Maintenant, nous sommes à la recherche de l'indice si un élément de ce groupe, dans notre cas c'est le premier élément du groupe ($ ( '. Maison »). Indice ($('. maison: premier'))).

Here's another example to help you better understand how index() works: Voici un autre exemple pour vous aider à mieux comprendre comment les index () fonctionne:

 $('.house').each(function(i){ $ ( '. maison »). chacun (function (i) ( 
      if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" if (i == 3) Maison ($('. d'alerte »). Index (this)); / / alertes 3, en supposant qu'il ya 3 ou plusieurs éléments avec class =" maison " 
 }); )); 

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. Donc, en gros, si vous avez besoin pour découvrir l'indice d'un élément dans un groupe, vous devez d'abord spécifier le groupe et puis passer l'objet dont vous avez besoin pour trouver l'indice comme un paramètre à la fonction index ().

Translate this post Translate this post


Related posts: Related posts:

  1. JavaScript: GIFless animation. JavaScript: GIFless animation. Animate images,logos with jQuery Animer des images, des logos avec jQuery
  2. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Envoyer fonction comme paramètre à une autre fonction (rappels)
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Que faire si jQuery animation ne se déclenche pas / start?
  4. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Où puis-je trouver toutes les propriétés à tous les éléments HTML?
  5. Javascript: How to validate email address with JavaScript? Javascript: Comment faire pour valider l'adresse e-mail avec JavaScript?

    blog comments powered by Disqus Commentaires Blog powered by Disqus