jQuery.inArray( value, array [, fromIndex ] )
来源:互联网 发布:seo转行前端开发 编辑:程序博客网 时间:2024/06/01 07:44
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
(没有找到) , 因为字符串数组中不可能找到一个数字:
1
$.inArray( 5 + 5, [ "8", "9", "10", 10 + "" ] );
例子:
返回数组中指定元素的索引值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!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>
Demo:
阅读全文
0 0
- jQuery.inArray( value, array [, fromIndex ] )
- jQuery中$.inArray(value,array,[fromIndex]))方法使用
- jQuery.inArray(value,array)
- jQuery.inArray(value,array)
- js inArray() jQuery.inArray( value, array )
- jquery $.inArray()
- jQuery.inArray
- 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()
- git学习地址
- java如何安全的延迟初始
- response.sendRedirect()与request.getRequestDispatcher().forward()区别
- LeetCode编程练习
- 手工部署Java Web项目【阿里云教程】
- jQuery.inArray( value, array [, fromIndex ] )
- eclipse使用教程(已安装jdk,eclipse,配好环境变量)
- php中 foreach引用(&)的问题
- php多维数组平均值
- java如何安全的延迟初始化
- static变量/方法,与基类父类执行的先后顺序
- Java多线程系列--“基础篇”08之 join()
- ubuntu14.04编译openjdk7
- Problems & Solutions -- 9 No Socket could be created