jQuery.inArray
来源:互联网 发布:wind mac版 excel插件 编辑:程序博客网 时间:2024/05/17 01:54
jQuery.inArray( value, array [, fromIndex ] )返回: Number
描述: 在数组中查找指定值并返回它的索引(如果没有找到,则返回-1)。
添加的版本: 1.2jQuery.inArray( value, array [, fromIndex ] )
- value类型: Anything要查找的值。
- array类型: Array一个数组,通过它来查找。
- fromIndex类型: Number数组索引值,表示从哪里在开始查找。默认值是0,这将查找整个数组。
$.inArray()
方法类似于JavaScript的原生.indexOf()
方法,没有找到匹配元素时它返回-1。如果数组第一个元素匹配value
(参数) ,那么$.inArray()
返回0。
因为JavaScript将0视为false(即 0 == false, 但是 0 !== false),要检查在array
中是否存在value
, 你需要检查它是否不等于(或大于)-1。
值之间的比较是严格比较(即,===或!==比较)。下面这段代码返回 -1
(没有找到) , 因为字符串数组中不可能找到一个数字:
$.inArray( 5 + 5, [ "8", "9", "10", 10 + "" ] );
例子:
<!DOCTYPE html><html><head> <style> div { color:blue; } span { color:red; }</style> <script src="http://code.jquery.com/jquery-latest.js"></script></head><body> <div>"John" found at <span></span></div><div>4 found at <span></span></div><div>"Karl" not found, so <span></span></div><div>"Pete" is in the array, but not at or after index 2, so <span></span></div><script>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> </body></html>
结果:
"John" found at 34 found at 0"Karl" not found, so -1"Pete" is in the array, but not at or after index 2, so -1
0 0
- jquery $.inArray()
- jQuery.inArray
- js inArray() jQuery.inArray( value, array )
- jQuery.inArray(value,array)
- jQuery.inArray(value,array)
- indexOf() 与 jquery.inArray()
- jquery inArray JSON解析
- jquery.inArray函数
- $.inArray()——JQuery
- jquery. inArray()函数详解
- jQuery.inArray() 函数详解
- jQuery.inArray() 函数详解
- jQuery.inArray() 函数详解
- jQuery.inArray() 函数详解
- jquery源码阅读jQuery.inArray()
- indexOf() lastIndexOf() 与 jQuery.inArray()
- jQuery.inArray方法注意点
- jQuery.inArray( value, array [, fromIndex ] )
- 二叉树
- 异常
- Spring配置文件xml头导致的错误
- js 跨域利用jsonp的原理
- 图论(七)--单源最短路
- jQuery.inArray
- 常用方法
- Anki记忆库的导入导出和制作
- U-Boot启动过程完全分析
- 对象引用
- U-Boot编译过程完全分析
- C++动态内存的开辟
- Python Numpy Tutorials: 改变数组的维度
- git中的README如何添加图片