JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hogyan juthat az index (position csoportján belül) egy objektum jQuery?
Posted on 20. Posted on 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Június, 2009 Dragos ben Kódolás, JavaScript és Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Még egy ideig még ma rájönni, hogyan kell, hogy megszerezze az indexet, és végül épp most zavarba ki.
Let's say we have a group of objects all containing the class=”house”. Tegyük fel, hogy van egy csoportja, amely tartalmazza az összes objektum class = "ház". 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: Most, ha mi viszonyítási az első eleme a csoporton belül, és szeretnénk megtudni, hogy az index (is nyilvánvaló, hogy ebben az esetben), mi ezt már használja ezt a kódrészletet:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. ház "). indexe ($('. ház: 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') ). Szóval, a kód a következőképpen működik: keresünk belül minden eleme egy csoport összes csoportosítani a class = "ház" ($ ( '. 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')) ). Most keresik az index, ha egy eleme ennek a csoportnak, a mi esetünkben ez az első eleme a csoport ($ ( '. House "). Indexe ($('. ház: first'))).
Here's another example to help you better understand how index() works: Itt egy újabb példa, hogy segít jobban megérteni, hogyan index () működése:
$('.house').each(function(i){ $ ( '. house "). 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 ($('. ház ".) index (e)); / / Alerts 3, feltételezve van 3 vagy több elemét a class =" ház " }); ));
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. Tehát alapvetően, ha szüksége van, hogy megtudja, az index egy elem a csoporton belül, akkor először megjelöli azt a csoportot, majd át a tárgyat meg kell találni az index, mint a paraméter az index () függvényt.
Related posts: Related posts:
- JavaScript: GIFless animation. A JavaScript: GIFless animáció. Animate images,logos with jQuery Animálás képeket, logókat és jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: mi van, ha jQuery animáció nem villan / start?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Hol találok Minden tulajdonság minden HTML elemek?
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send függvény paraméterként egy másik funkció (visszahívások)
- JavaScript: Get anchor from URL JavaScript: Get horgony az URL-ből












































