jQuery.inArray () method
Examples
The index value of the specified element in the returned array
<Div> "John" in the index value of <span> </ span> location is found </ div>
<Div> 4 was found in the index value of <span> </ span> position </ div>
<Div> "Karl" is not found, it returns the <span> </ span> </ Div>
<Div> "Pete" in the array, but not the position of the index value is greater than 2, it returns <span> </ span> < / div>
<Script> $ (function () {
var arr = [ 4, "Pete", 8, "John" ]; Var $ spans = $ ( "Span" );. $ Spans eq ( 0 ). Text ( jQuery. inArray ( "John", arr ) );. $ Spans eq ( 1 ). Text ( jQuery. inArray ( 4, arr ) );. $ Spans eq ( 2 ). Text ( jQuery. inArray ( "Karl", arr ) );. $ Spans eq ( 3 ). Text ( jQuery. inArray ( "Pete", arr, 2 ) );}) </ Script>
try it"
Definition and Usage
$ .inArray () Function is used to find the specified value in the array, and returns its index value (if not found, -1)
Tip: source array is not affected, filter results reflect only the results of the array returned.
grammar
$.inArray( value, array [, fromIndex ] )
参数 | 描述 |
---|---|
value | 任意类型 用于查找的值。 |
array | Array类型 指定被查找的数组。 |
fromIndex | 可选。Number类型 指定从数组的指定索引位置开始查找,默认为 0 |