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 Uppdateringar via RSS | E-post Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… Hem / Kodning / JavaScript / Ajax / JavaScript: Hur får jag ...

JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hur man får indexet (position inom en grupp) för ett objekt med jQuery?

Posted on 20. Skrivet den 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Juni, 2009 av Dragos i Coding, JavaScript och Ajax

I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Jag har spenderat lite tid idag klura ut hur man får detta index och till slut har jag bara förbryllad ut.

Let's say we have a group of objects all containing the class=”house”. Låt oss säga att vi har en grupp av alla objekt som innehåller class = "hus". 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: Om vi nu är hänvisningarna till första elementet inom gruppen och vi vill ta reda på det index (och det är uppenbart i detta fall), skulle vi använda denna kodsnutt:

 var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; Var indexOfFirstElement =$('. hus "). index ($('. house: 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') ). Så fungerar koden enligt följande: vi söker i alla delar av en grupp alla grupperade med class = "hus" ($ ( ". House ')). 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')) ). Nu söker vi för index om ett element inom denna grupp, i vårt fall är det den första delen av koncernen ($ ( '. Hus "). Index ($('. house: first"))).

Here's another example to help you better understand how index() works: Här är ett annat exempel som hjälper dig att bättre förstå hur index () fungerar:

 $('.house').each(function(i){ $ ( '. hus "). 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) alert ($('. hus "). index (this)); / / Alerts 3, antar det finns 3 eller flera delar med class =" hus " 
 }); )); 

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. Så i princip, om du behöver ta reda på index för ett element i en grupp, bör du ange den första gruppen och sedan vidarebefordra det objekt som du behöver för att hitta index som en parameter till index () funktion.

Translate this post Översätta det här inlägget


Related posts: Relaterade tjänster:

  1. JavaScript: GIFless animation. JavaScript: GIFless animation. Animate images,logos with jQuery Animera bilder, logotyper med jQuery
  2. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Vad händer om jQuery animation inte eld / start?
  3. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Var hittar jag alla egenskaper för alla HTML-element?
  4. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Skicka fungera som en parameter till en annan funktion (callbacks)
  5. JavaScript: Get anchor from URL JavaScript: Få ankare från URL

    blog comments powered by Disqus blogg kommentarer drivs av Disqus