JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Come ottenere l'indice (posizione all'interno di un gruppo) di un oggetto con jQuery?
Posted on 20. Posted on 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Giugno, 2009 da Dragos in Coding, JavaScript e Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Ho trascorso un certo tempo di oggi per capire come ottenere tale indice e, infine, ho appena perplesso it out.
Let's say we have a group of objects all containing the class=”house”. Diciamo che abbiamo un gruppo di oggetti che contengono tutte le class = "casa". 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: Ora, se fanno riferimento al primo elemento all'interno del gruppo e ci piacerebbe sapere che è indice di (e si vede, in questo caso), abbiamo usato questo pezzo di codice:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. casa '). index ($('. casa: prima')); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Così, il codice funziona nel modo seguente: abbiamo una ricerca all'interno di tutti gli elementi di un gruppo, raggruppati per la class = "casa" ($ ( '. Casa')). 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')) ). Ora stiamo cercando l'indice, se un elemento all'interno di questo gruppo, nel nostro caso è il primo elemento del gruppo ($ ( '. Casa'). Index ($('. casa: prima '))).
Here's another example to help you better understand how index() works: Ecco un altro esempio per aiutarti a capire meglio come index () funziona:
$('.house').each(function(i){ $ ( '. casa'). ciascun (function (i) ( if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" if (i == 3) casa ($('. alert '). index (this)); / / avvisi 3, supponendo che ci sono 3 o più elementi con class = "casa" }); ));
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. Quindi, fondamentalmente, se avete bisogno di scoprire l'indice di un elemento all'interno di un gruppo, è necessario prima specificare il gruppo e poi passare l'oggetto è necessario trovare l'indice di come parametro per la funzione index ().
Related posts: Related posts:
- JavaScript: GIFless animation. JavaScript: animazione GIFless. Animate images,logos with jQuery Animare le immagini, i loghi con jQuery
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Invia funzione come parametro a un'altra funzione (callback)
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: jQuery Che cosa succede se l'animazione non viene generato / start?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Dove posso trovare tutte le proprietà di tutti gli elementi HTML?
- Javascript: How to validate email address with JavaScript? Javascript: Come validare indirizzo email con JavaScript?












































