JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Miten saada indeksi (asema ryhmä) objektin kanssa jQuery?
Posted on 20. Postitettu 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Kesäkuu, 2009 Dragos on Coding, 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 viettänyt aikaa tänään miettiminen, miten saada hakemistosta ja lopuksi olen aivan ymmälläni sen.
Let's say we have a group of objects all containing the class=”house”. Sanotaan, että meillä on ryhmä vastustaa kaikki sisältävät class = "talo". 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: Nyt kun olemme viitetietoina on ensimmäinen osa konsernin sisällä, ja haluaisimme tietää, että indeksin (ja se on selvää, tässä tapauksessa), emme halua käyttää tätä koodinpätkä:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. talo "). indeksi ($('. talo: ensimmäinen ')); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Niin, koodi toimii seuraavasti: me etsit kaikissa osa konsernin kaikki ryhmiteltyinä class = "talo" ($ ( '. 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')) ). Nyt etsimme indeksi, jos osa tästä ryhmästä, ja tässä tapauksessa se on ensimmäinen osa ryhmän ($ ( '. House "). Indeksi ($('. talo: ensimmäinen'))).
Here's another example to help you better understand how index() works: Tässä on toinen esimerkki auttaa sinua ymmärtämään paremmin, miten indeksi () teoksia:
$('.house').each(function(i){ $ ( '. house "). kutakin (toiminto (i) ( if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" if (i == 3) alert ($('. house "). indeksi (tämä)), / / Alerts 3, olettaen että on 3 tai enemmän elementtien class =" talo " }); ));
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. Joten periaatteessa, jos haluat selvittää indeksi osa konsernin sisällä, on ensin määriteltävä ryhmä ja sitten siirtää objektin löydettävä indeksi parametrina indeksi ()-funktiota.
Related posts: Liittyvien virkojen:
- JavaScript: GIFless animation. JavaScript: GIFless animaatio. Animate images,logos with jQuery Animoida kuvia, logoja ja jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Mitä jos jQuery animaatio ei palo / alku?
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Lähetä toimivat parametri toisen toiminnon (kutsuja)
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Mistä löydän kaikki ominaisuudet kaikki HTML-elementit?
- JavaScript: Get anchor from URL JavaScript: Hanki ankkuri URL-osoitteesta












































