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 6月、2009 符号化 、 の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;ヴァール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')) ).ここでは、インデックスを探している場合、我々の場合は、このグループ内では、グループの最初の要素($('。house'での要素)。指数($('.ハウス:最初の')))。
Here's another example to help you better understand how index() works:ここでは、より良い方法をインデックス()の動作を理解するため別の例です:
$('.house').each(function(i){ $('。ハウス')、各(関数(1)( if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house"場合、(私は== 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.したがって、基本的に、もしあなたがグループ内の要素のインデックスを見つける必要がある場合、最初にして、あなたのインデックスを検索する必要があるオブジェクトを渡すことは、グループを指定する必要があるインデックスをパラメータとして()関数。
Related posts:関連記事:
- JavaScript: GIFless animation. JavaScriptを:GIFlessアニメーション。 Animate images,logos with jQuery アニメーション化、画像、jQueryを使ってのロゴ
- JavaScript: Send function as a parameter to another function (callbacks) JavaScriptを:別の関数(コールバック)へのパラメータとして関数を送る
- JavaScript: What if jQuery animation doesn't fire/start? JavaScriptを:どのような場合はjQueryのアニメーションは火災/起動するのですか?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScriptを:どこで 、 すべてのHTML要素のためのすべてのプロパティを探すのですか?
- Javascript: How to validate email address with JavaScript? Javascriptを:どのようにJavaScriptを使って電子メールアドレスを検証する?












































