JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Jak se dostat na indexu (pozici v rámci skupiny) z objektu s jQuery?
Posted on 20. Publikováno dne 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Června 2009 od Dragos v Kódování, 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 jsem nějaký čas dnes najít způsob, jak dosáhnout tohoto indexu, a nakonec jsem jen nechápavě to.
Let's say we have a group of objects all containing the class=”house”. Řekněme, že máme skupinu předmětů, které obsahují všechny 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: Nyní máme-li odkazující na první prvek ve skupině a my bychom se chtěli dozvědět, že index (dobře je to vidět v tomto případě), měli bychom používat tento kus kódu:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. dům '). ($('. index domu: první')); / / 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 takto: budeme hledat ve všech prvků skupiny všech seskupené podle class = "dům" ($ ( '. 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')) ). Nyní hledáme indexu, pokud prvek v rámci této skupiny, v našem případě to je první prvek ze skupiny ($ ( '. Domu'). ($('. Index domu: první '))).
Here's another example to help you better understand how index() works: Zde je další příklad, který vám pomůže lépe pochopit, jak 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 ($('. dům '). indexu (this)); / / upozornění na 3, za předpokladu, že jsou 3 nebo více prvků s class = "dům" }); ));
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 podstatě, pokud potřebujete zjistit index prvku ve skupině, měli byste nejprve určit skupinu a poté projít objekt musíte najít indexu jako parametr index () funkce.
Related posts: Související příspěvky:
- JavaScript: GIFless animation. JavaScript: GIFless animace. Animate images,logos with jQuery Animovat obrázky, loga s jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Co když jQuery animace není oheň / start?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Kde najdu všechny vlastnosti pro všechny prvky HTML?
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Poslat funkci jako parametr jiné funkce (volání)
- JavaScript: Get anchor from URL JavaScript: Get kotva z URL












































