0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

indexメソッドとは

Posted at

indexメソッドとは

index()メソッドは、jQueryで使用されるメソッドの一つで、要素が兄弟要素の中で何番目に位置しているかを取得するために使用されます。

使用方法

以下の例では、li要素の中の.activeクラスのインデックス番号を取得できます。.activeクラスが複数ある場合は最初の要素のインデックス番号を取得します。
また、一致する要素が見つからない場合は、-1が返されます。

<ul>
    <li>インデックス0</li>
    <li class="active">インデックス1</li>
    <li>インデックス2</li>
</ul>
// activeクラスを持った要素はli要素の2つ目なのでインデックス番号の1が返る
$('li').index($('.active'));
// => 1

// sampleクラスを持ったli要素は存在しないため-1が返る
$('li').index($('.sample'));
// => -1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?