This is a translated page. The original can be found here: http://iwebdevel.com/2009/06/20/javascript-how-to-get-the-index-position-within-a-group-of-an-object-with-jquery/
UPDATES VIA RSS | Email עדכונים דרך RSS | אימייל Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… דף הבית / Coding / JavaScript & אייאקס / JavaScript: איך להשיג את אני ...

JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: כיצד לקבל את האינדקס (עמדת בתוך קבוצה) של אובייקט עם jQuery?

Posted on 20. פורסם ב 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax יוני, 2009 על ידי Dragos ב וקידוד, JavaScript & אייאקס

I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. ביליתי היום קצת זמן להבין איך להשיג כי מדד ולבסוף אני פשוט מבולבלת זה.

Let's say we have a group of objects all containing the class=”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: עכשיו אם אנחנו התייחסות לאלמנט הראשון בקבוצת והיינו רוצים לגלות מדד זה (טוב זה ברור במקרה זה), היינו משתמשים זה חתיכת קוד:

 var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; indexOfFirstElement var =$('. הבית "). לאינדקס ($('. הבית: הראשון")); / / indexOfFirstElement = 0; 

So, the code works as follows: we're searching within all elements of a group all grouped by the class=”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')) ). עכשיו אנחנו מחפשים מדד אם אלמנט בתוך קבוצה זו, במקרה שלנו זה האלמנט הראשון של הקבוצה ($ ( '. בית'). לאינדקס ($('. הבית: הראשון "))).

Here's another example to help you better understand how index() works: הנה דוגמה נוספת כדי לעזור לך להבין טוב יותר כיצד המדד () עובד:

 $('.house').each(function(i){ $ ( '. בית'). כל (פונקציה (אני) ( 
      if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" if (i == 3) בית ($('. ההתראה "). לאינדקס (זה)); / / התראות 3, נניח שיש 3 או יותר אלמנטים עם class =" בית " 
 }); )); 

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. אז בעצם, אם אתה צריך למצוא את האינדקס של אלמנט בתוך קבוצה, תחילה עליך להגדיר את הקבוצה ולאחר מכן להעביר את האובייקט אתה צריך למצוא את האינדקס של כפרמטר למדד () function.

Translate this post תרגם פירסום


Related posts: הודעות קשורות:

  1. JavaScript: GIFless animation. JavaScript: אנימציה GIFless. Animate images,logos with jQuery הנפשת תמונות, לוגו עם jQuery
  2. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: מה אם האנימציה jQuery לא אש / להתחיל?
  3. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: איפה אני מוצא כל מאפיינים עבור כל האלמנטים של HTML?
  4. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: שלח פונקציה כפרמטר לפונקציה אחרת (callbacks)
  5. JavaScript: Get anchor from URL JavaScript: קבל עוגן מתוך כתובת

    blog comments powered by Disqus תגובות לבלוג powered by Disqus