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 | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: How to get the i… Головна / кодування / JavaScript & Ajax / 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 & Ajax

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; VAR indexOfFirstElement =$('. дім '). Індексом ($('. будинку: перший')) / / 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: Ось ще один приклад, щоб допомогти вам краще зрозуміти, яким Index () працює:

 $('.house').each(function(i){ $ ( '. Дім "). Кожного з них (функція (I) (
     if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" якщо (I == 3) Оповіщення будинку ($('. '). індексом (це)); / / Alerts 3, вважаючи, є 3 або більше елементів з класом = "Дім"
}); )); 

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. Таким основному, якщо ви хочете дізнатися індекс елементу в групі, ви повинні спочатку вказати групу, а потім передати об'єкт вам потрібно буде знайти індекс як параметр Index () функцію.

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: Send function as a parameter to another function (callbacks) JavaScript: Надіслати функцію як параметр в іншу функцію (функції зворотного виклику)
  4. JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: Де мені знайти властивості для всіх елементів HTML?
  5. JavaScript: Get anchor from URL JavaScript: Отримати якір, з URL

    blog comments powered by Disqus Блог коментарі сила Disqus