JavaScript: How to get the index (position within a group) of an object with jQuery? Javascript: Kuidas saada indeks (seisukoha rühma) objekti jQuery?
Posted on 20. Postitatud 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Juuni, 2009 Dragos in kodeerimise, JavaScript ja Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Olen mõnda aega täna figuring kuidas saada, et indeks ja lõpuks ma olen lihtsalt hämmingus seda.
Let's say we have a group of objects all containing the class=”house”. Oletame, et meil rühma objektid kõik sisaldavad class = "maja". 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: Nüüd, kui meil on viitamine esimese osa kontserni sees ja me tahaksime teada, see indeks (ka see on selge, sel juhul), suudaksime seda tükk kood:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. maja "). indeks ($('. maja: esimene")); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Nii, kood toimib järgmiselt: me otsitava jooksul kõiki osi rühmitada kõik rühmitatud class = "maja" ($ (. "Maja")). 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')) ). Nüüd otsivad indeks kui element selles rühmas, meie puhul on see esimene osa rühma ($ (. "Maja"). Indeks ($('. maja: esimene "))).
Here's another example to help you better understand how index() works: Siin on üks näide, mis aitavad teil paremini mõista, kuidas indeks () tööd:
$('.house').each(function(i){ $ (. "maja"). 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) teate ($('. maja "). index (this)); / / Alerts 3, arvata on 3 või rohkem elemente class =" maja " }); ));
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. Nii et põhimõtteliselt, kui Sul on vaja leida läbi indeksiga element grupp, peaksite kõigepealt kindlaks grupp ja seejärel liigu objekti Sul on vaja leida indeksi kui parameeter indeks () funktsiooni.
Related posts: Seonduvad postitused:
- JavaScript: GIFless animation. Javascript: GIFless animatsioon. Animate images,logos with jQuery Animeeritud piltide, logode jQuery
- JavaScript: What if jQuery animation doesn't fire/start? Javascript: Mida teha, kui jQuery animatsioon ei tule alustada?
- JavaScript: Where do I Find All Properties for All HTML Elements ? Javascript: Kust leida kõik omadused Kõik HTML elemendid?
- JavaScript: Send function as a parameter to another function (callbacks) Javascript: Saada funktsiooni parameeter teise funktsiooni (kutse,)
- JavaScript: Get anchor from URL JavaScript: Get kinnistada saadud URL












































