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… 首页 / 编码 / 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 2008年6月,2009年在编码 ,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 =$('.房子')。指数($('.房子:第一')); / / 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"如果(1 == 3)警报($('.房子')。指数(本)); / /警报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.所以基本上,如果你需要找出集团内一个元素的索引,您应该先指定组,然后传递对象,你需要寻找的指数作为指数参数()函数。

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的:寄另一个函数(回调参数功能)
  5. JavaScript: Get anchor from URL JavaScript的:从网址锚

    blog comments powered by Disqus 博客评论 Disqus