JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Wie in den Index zu erhalten (Position innerhalb einer Gruppe) eines Objekts mit jQuery?
Posted on 20. Gesendet am 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Juni 2009 von Dragos in Coding, JavaScript und Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Ich habe heute einige Zeit damit verbracht herauszufinden, wie man diesen Index zu erhalten und endlich habe ich es eben verwirrt aus.
Let's say we have a group of objects all containing the class=”house”. Nehmen wir an, wir haben eine Gruppe von Objekten, die alle mit class = "Haus". 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: Nun, wenn wir verweisen auf das erste Element innerhalb der Gruppe, und wir möchten es aus dem Verbraucherpreisindex (well it's in diesem Fall offensichtlich zu finden), dann würden wir nutzen dieses Stück Code:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. Haus "). Index ($('. Haus: 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') ). So funktioniert der Code wie folgt: Wir suchen in allen Elementen einer Gruppe alle von der Klasse zusammengefasst = "Haus" ($ ( '. Haus')). 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')) ). Jetzt suchen wir für den Index, wenn ein Element in dieser Gruppe, in unserem Fall ist es das erste Element der Gruppe ($ ( '. Haus "). Index ($('. Haus: Erste'))).
Here's another example to help you better understand how index() works: Hier ein weiteres Beispiel zu helfen, besser zu verstehen, wie Index-Werke ():
$('.house').each(function(i){ $ ( '. Haus "). 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) Ausschreibung ($('. Haus "). Index (this)); / / 3 erhalten, vorausgesetzt, es gibt 3 oder mehr Elemente mit class =" Haus " }); ));
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. So im Allgemeinen, wenn Sie herausfinden müssen, den Index ein Element innerhalb einer Gruppe, sollten Sie als erstes die Gruppe und dann die Objekt, das Sie brauchen, um den Index zu finden als Parameter den Index ()-Funktion.
Related posts: In Verbindung stehende Pfosten:
- JavaScript: GIFless animation. JavaScript: GIFless Animation. Animate images,logos with jQuery Animieren von Bildern, Logos mit jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Was passiert, wenn jQuery Animation nicht Feuer / start?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Wo finde ich alle Eigenschaften für alle HTML-Elemente?
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send-Funktion als Parameter an eine andere Funktion (Rückrufe)
- JavaScript: Get anchor from URL JavaScript: Get Anker von URL












































