jQuery.inArray()方法

jQuery 教程 · 2019-03-29 08:57:40

实例

返回数组中指定元素的索引值

<div>"John" 在索引值为 <span></span> 的位置被找到</div> <div>4 在索引值为 <span></span> 的位置被找到</div> <div>"Karl" 未被找到,所以返回 <span></span> </div> <div>"Pete" 在数组中,但是不在索引值大于2的位置,所以返回 <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>

定义和用法

$.inArray() 函数用于在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1)

提示:源数组不会受到影响,过滤结果只反映在返回的结果数组中。

语法

$.inArray( value, array [, fromIndex ] )

参数 描述
value 任意类型 用于查找的值。
array Array类型 指定被查找的数组。
fromIndex 可选。Number类型 指定从数组的指定索引位置开始查找,默认为 0

点击查看所有 jQuery 教程 文章: https://www.codercto.com/courses/l/35.html

查看所有标签

Algorithms to Live By

Algorithms to Live By

Brian Christian、Tom Griffiths / Henry Holt and Co. / 2016-4-19 / USD 30.00

A fascinating exploration of how insights from computer algorithms can be applied to our everyday lives, helping to solve common decision-making problems and illuminate the workings of the human mind ......一起来看看 《Algorithms to Live By》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具