JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kaip gauti indeksą (pozicijos grupės) su jQuery objektas?
Posted on 20. Posted on 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Birželis, 2009 Dragos ir kodavimo, JavaScript ir Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Aš praleido šiek tiek laiko šiandien suprasti, kaip galima gauti indeksą, ir kad pagaliau aš tiesiog suglumino jį.
Let's say we have a group of objects all containing the class=”house”. Tarkime, kad turime daiktų, kurių sudėtyje yra visos klasės grupėje = "namas". 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: Dabar, jei mes nuorodas į pirmąjį elementą grupėje ir mes norėtume sužinoti jo indeksą (ir tai akivaizdu, šiuo atveju), mes būtume naudoti šį kodo fragmentą:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; var indexOfFirstElement =$('. namas "). indeksas ($('. namas: pirmoji ')); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Taigi, kodas veikia taip: mes ieškote per visą grupės visi sugrupuoti pagal class = "namai" ($ ( ". House") elementai). 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')) ). Dabar mes ieškome indeksas, jei šioje grupėje elementas, mūsų atveju tai pirmas elementas grupės ($ ( ". House"). Indeksas ($('. namas: pirmasis "))).
Here's another example to help you better understand how index() works: Štai dar vienas pavyzdys, kad padėtų jums geriau suprasti, kaip index () darbų atveju:
$('.house').each(function(i){ $ ( '. house "). kiekvienai (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 ($('. house "). indekso (this)); / / Alerts 3, galvoju, yra 3 ar daugiau elementų, class =" namas " }); ));
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. Taigi, iš esmės, jei Jums reikia sužinoti elemento indeksas per grupę, pirmiausia turėtumėte nurodyti grupę, tada perkelti objektą, kurį reikia surasti indeksą, kaip parametro indeksą () funkciją.
Related posts: Related posts:
- JavaScript: GIFless animation. JavaScript: GIFless animacija. Animate images,logos with jQuery Gyva nuotraukų, logotipų su jQuery
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Siųsti funkcija kaip parametro kitos funkcijos (Atgalinės iššūkius)
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Ką daryti, jei jQuery animacija nėra gaisro / paleisti?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Kur rasti Visi Ypatybės Visi HTML elementai?
- Javascript: How to validate email address with JavaScript? Javascript: Kaip patvirtinti elektroninio pašto adresą su "JavaScript?












































