JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kif tista 'tikseb l-indiċi (il-pożizzjoni fi ħdan grupp) ta' oġġett jQuery?
Posted on 20. Posted on 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax Ġunju, 2009 minn Dragos fl-Kodifika, JavaScript & Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. Stajt jintefqu xi żmien llum jidhru kif tikseb dik l-indiċi u fl-aħħar stajt biss puzzled out.
Let's say we have a group of objects all containing the class=”house”. Ejja ngħidu li għandna grupp ta 'oġġetti kollha li fihom il-klassi = "house". 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: Issa jekk irridu referenzar l-ewwel element fi ħdan il-grupp u we'd tixtieq issir taf indiċi huwa (ukoll huwa evidenti f'dan il-każ), we'd użu din il-biċċa tal-kodiċi:
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; indexOfFirstElement var =$('. house "). indiċi ($('. dar: l-ewwel")); / / indexOfFirstElement = 0; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). Għalhekk, il-kodiċi tax-xogħlijiet kif ġej: we're tiftix fl-elementi kollha ta 'grupp kollha miġbura mill-klassi = "house" ($ ( ". 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')) ). Issa aħna qegħdin ifittxu l-indiċi, jekk element fi ħdan dan il-grupp, fil-każ tagħna huwa l-ewwel element tal-grupp ($ ( ". House"). Indiċi ($('. dar: l-ewwel "))).
Here's another example to help you better understand how index() works: Hawn eżempju ieħor biex jgħinek tifhem aħjar kif indiċi () xogħlijiet:
$('.house').each(function(i){ $ ( ". house"). f'kull (funzjoni (i) ( if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" jekk (i == 3) allert ($('. house "). indiċi (dan)); / / allerti 3, jekk jitqies hemm 3 jew aktar mill-elementi tal-klassi =" house " }); ));
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. Għalhekk bażikament, jekk ikollok bżonn issib l-indiċi ta 'element fi ħdan grupp, l-ewwel għandek tispeċifika l-grupp u imbagħad jgħaddu l-oġġett ikollok bżonn issib l-indiċi tal bħala parametru għall-indiċi () funzjoni.
Related posts: Related postijiet:
- JavaScript: GIFless animation. JavaScript: animazzjoni GIFless. Animate images,logos with jQuery Janimaw immaġini, logos jQuery
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send funzjoni bħala parametru ieħor għall-funzjoni (callbacks)
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: X'jiġri jekk jQuery animazzjoni ma 'nar / tibda?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Fejn nista Find All Properties for All Elementi HTML?
- Javascript: How to validate email address with JavaScript? Javascript: Kif biex jivvalida l-indirizz email bl JavaScript?












































