JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Ako sa dostať na indexe (pozíciu v rámci skupiny) z objektu s jQuery?
Posted on 20. Publikované dňa 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Júna 2009 od Dragos v Kódovanie, JavaScript & Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Strávil som nejaký čas dnes nájsť spôsob, ako dosiahnuť tohto indexu, a nakoniec som len nechápavo to.
Let's say we have a group of objects all containing the class=”house”. Povedzme, že máme skupinu predmetov, ktoré obsahujú všetky class = "domu". 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: Teraz máme-li odkazujúce na prvý prvok v skupine a my by sme sa chceli dozvedieť, že index (dobre je to vidieť v tomto prípade), mali by sme používať tento kus kódu:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. dom '). ($('. index domu: prvá')); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Takže kód funguje nasledovne: budeme hľadať vo všetkých prvkov skupiny všetkých zoskupené podľa class = "dom" ($ ( '. Domu')). 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')) ). Teraz hľadáme indexu, ak prvok v rámci tejto skupiny, v našom prípade to je prvý prvok zo skupiny ($ ( '. Domu'). ($('. Index domu: prvý'))).
Here's another example to help you better understand how index() works: Tu je ďalší príklad, ktorý vám pomôže lepšie pochopiť, ako index () funguje:
$('.house').each(function(i){ $ ( '. Domu'). 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) záznam ($('. dom '). indexu (this)); / / upozornenie na 3, za predpokladu, že sú 3 alebo viac prvkov s class = "dom" }); ));
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. Takže v podstate, ak potrebujete zistiť index prvku v skupine, mali by ste najprv určiť skupinu a potom prejsť objekt musíte nájsť indexu ako parameter index () funkcie.
Related posts: Súvisiace príspevky:
- JavaScript: GIFless animation. JavaScript: GIFless animácie. Animate images,logos with jQuery Animovať obrázky, logá s jQuery
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Poslať funkcii ako parameter inej funkcie (volanie)
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Čo keď jQuery animácie nie je oheň / start?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Kde nájdem všetky vlastnosti pre všetky prvky HTML?
- Javascript: How to validate email address with JavaScript? Javascript: Ako overiť e-mailovú adresu JavaScript?












































