网站制作学习网JS脚本→正文:jquery获取对象在数组对象中的索引值
字体:

jquery获取对象在数组对象中的索引值

JS脚本 2012/11/23 10:07:01  点击:不统计

学习www.网for站asp制.cn作

在jquery中有通过索引获取该jquery对象的,单如何通过对象获取该对象所在的索引呢?

jquery提供了index(jqueryObj对象)的方法该方法的返回值是索引值,当对象数组中没有该对象时,

返回的是-1.
语法
var index = $(".class").index($("#id"));
上面的语法意思是:在所有.class类对象中找到id为"id"的jquery对象的索引
看实例代码:
HTML代码
<div class="forasp" id="www">网站</div>
<div class="forasp" id="forasp">制作</div>
<div class="forasp" id="dian">学习</div>
<div class="forasp" id="cn">forasp.cn</div>
jquery代码
var obj_a  = $(".forasp");
alert(obj_a.index($("#www")));
alert(obj_a.index($("#dian")));
alert(obj_a.index($("#notExists")));

上面的返回值分别是 0 2 和-1.

这样就获得了jquery对象在数组中的索引。


·上一篇:jquery对象转换成Dom对象 >>    ·下一篇:javascript replace 正则表达式有变量 >>
推荐文章
最新文章