ドキュメント

Utilities/jQuery.inArray

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.inArray( value, array )

Determine the index of the first parameter in the Array (-1 if not found).
Arguments:
valueAny
Value to see if it exists in the array.
arrayArray
Array to look through for the value.
Examples:


    var arr = [ 4, "Pete", 8, "John" ];

    $("span:eq(0)").text(jQuery.inArray("John", arr));
    $("span:eq(1)").text(jQuery.inArray(4, arr));
    $("span:eq(2)").text(jQuery.inArray("David", arr));

<!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(){
    
    var arr = [ 4, "Pete", 8, "John" ];

    $("span:eq(0)").text(jQuery.inArray("John", arr));
    $("span:eq(1)").text(jQuery.inArray(4, arr));
    $("span:eq(2)").text(jQuery.inArray("David", arr));

  });
  </script>
  <style>
  div { color:blue; }
  span { color:red; }
  </style>
</head>
<body>
  <div>"John" found at <span></span></div>
  <div>4 found at <span></span></div>
  <div>"David" found at <span></span></div>
</body>
</html>

NameType