ドキュメント

Traversing/eq

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Traversing

eq( index )

Reduce the set of matched elements to a single element.
Argument is the position of the element in the set of matched elements, starting at 0 and going to length - 1. Since the query filters out all elements that do not match the given index, providing an invalid index returns an empty set of elements rather than null.
Arguments:
indexInteger
The index of the element in the jQuery object.
Examples:

Turn the div with index 2 blue by adding an appropriate class.

    $("div").eq(2).addClass("blue");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("div").eq(2).addClass("blue");

  });
  </script>
  <style>
  div { width:60px; height:60px; margin:10px; float:left;
        border:2px solid blue; }
  .blue { background:blue; }
  </style>
</head>
<body>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</body>
</html>

NameType