JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hvordan få indeksen (posisjon innenfor en gruppe) av et objekt med jQuery?
Posted on 20. Skrevet av 20.. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Jun 2009 av Dragos i Coding, JavaScript og Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Jeg har brukt litt tid i dag finne ut hvordan du skaffer at indeksen og til slutt må jeg bare forundret ut.
Let's say we have a group of objects all containing the class=”house”. La oss si at vi har en gruppe av objekter alle inneholder class = "huset". 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å hvis vi henviser til det første elementet i gruppen, og vi ønsker å finne ut det er indeksen (vel det er opplagt i dette tilfellet), vil vi bruke denne kodebit:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. hus '). indeks ($('. hus: 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') ). Så fungerer koden slik: vi søker innenfor alle elementer av en gruppe alle gruppert etter class = "huset" ($ ( '. Hus')). 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å ser vi for indeksen hvis et element innenfor denne gruppen, i vårt tilfelle er det det første elementet i gruppen ($ ( '. Hus'). Indeks ($('. hus: first '))).
Here's another example to help you better understand how index() works: Her er et eksempel for å hjelpe deg å bedre forstå hvordan indeks () fungerer:
$('.house').each(function(i){ $ ( '. hus'). hvert (funksjon (i) ( if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" if (i == 3) alert ($('. hus '). indeks (this)); / / varsler 3, inntar det er 3 eller flere elementer med class = "huset" }); ));
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. Så i utgangspunktet, hvis du trenger å finne ut av indeksen til et element i en gruppe, bør du først angi gruppen og deretter vedta objektet må du finne indeksen på som en parameter til indeksen ()-funksjonen.
Related posts: Relaterte artikler:
- JavaScript: GIFless animation. JavaScript: GIFless animasjon. Animate images,logos with jQuery Animere bilder, logoer med jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Hva hvis jQuery animasjon ikke brann / start?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Hvor finner jeg alle egenskaper for alle HTML-elementer?
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send fungere som en parameter til en annen funksjon (tilbakeanrop)
- JavaScript: Get anchor from URL JavaScript: Få anchor fra webadresse












































