JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kako biste dobili indeks (poziciju unutar grupe) s jQuery objekt?
Posted on 20. Posted on 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Jun, 2009 by Dragos u kodiranju, Ajax & JavaScript
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Ja sam proveo neko vrijeme danas figuring out kako nabaviti taj indeks i na kraju upravo sam ga zbunjeni out.
Let's say we have a group of objects all containing the class=”house”. Recimo mi imamo grupu objekata svih sadrže class = "kuća". 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: Sada, ako mi se upućuje na prvi element unutar grupe te smo željeli saznati da je indeks (dobro to je očito u ovom slučaju), mi bismo koristiti ovaj dio koda:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. kuća '). indeks ($('. kuće: prva ")); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Dakle, kod radova kao što slijedi: we're searching unutar svih elemenata od svih grupa grupiranih po class = "house" ($ ( '. Kuća')). 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')) ). Sada smo u potrazi za indeks, ako elementa unutar ove grupe, u našem slučaju to je prvi element iz skupine ($ ( '. Kuća'). Indeks ($('. kuće: prva "))).
Here's another example to help you better understand how index() works: Evo još jedan primjer kako bi vam pomoći da bolje razumiju kako indeks () radi:
$('.house').each(function(i){ $ ( '. kuća'). 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 ($('. kuća '). indeks (ova)); / / Alerts 3, misleći tu su 3 ili više elemenata s class = "house" }); ));
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. Dakle u osnovi, ako trebate saznati indeks elementa unutar grupe, prvo biste trebali navesti grupu, a zatim prijeđite objekt morate pronaći indeks kao parametar indeks () funkcije.
Related posts: Related posts:
- JavaScript: GIFless animation. JavaScript: GIFless animacija. Animate images,logos with jQuery Animate slike, logotipi s jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Što ako jQuery animacija ne požara / početi?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Gdje mogu pronaći Sve nekretnine za sve HTML elemenata?
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Pošaljite funkcija kao parametar za druge funkcije (callbacks)
- JavaScript: Get anchor from URL JavaScript: Get sidro iz URL-a












































