问题描述:灵活使用jquery中的index方法

方法签名:index([selector|element])

用法概述:P1.index(P2)  //调用者P1可以为对象或集合

  1. 参数为空,返回P1[0]在父容器内的索引;
  2. 参数为字符串(即传入了一个选择器selector),返回P1[0]在P2的所有匹配中的索引,即只要传入字符串,就是在P2匹配的元素中查找P1[0];
  3. 参数为js对象或jquery对象(即传入了一个或一组element),则返回P2[0]在P1中的索引,即只要传入的是对象,就是在P1中查找P2[0];和传入字符串的情况正好相反;

使用实例:

<ul>
    <li class="foo">Outer1</li>
    <li>
        <ul>
            <li>1</li>
            <li id="ok" class="foo">2</li>
            <li>3</li>
            <li class="foo">4</li>
        </ul>
    </li>
    <li>Outer3</li>
</ul>
$("li").index("#ok");     //-1,找不到,因为是在匹配"#ok"的元素中查找$("li")[0]
$("li").index($("#ok"));
$("li").index(".foo");    //0//更多情况可自行测试和研究

jQuery源码: (推荐:)

// Determine the position of an element within
// the matched set of elements
index: function( elem ) {
    // No argument, return index in parent
    if ( !elem ) {
        return ( this[ 0 ] && this[ 0 ].parentNode ) ? this.first().prevAll().length : -1;
    }
    // index in selector
    if ( typeof elem === "string" ) {
        return jQuery.inArray( this[ 0 ], jQuery( elem ) );
    }
    // Locate the position of the desired element
    return jQuery.inArray(
        // If it receives a jQuery object, the first element is used
        elem.jquery ? elem[ 0 ] : elem, this );
}

jquery中的index方法的更多相关文章

  1. jQuery中的index方法介绍

    从jq api手册摘过来的内容,index这个方法在写 tab silder 之类的组件还是比较有用的说. js没有传统的函数重载的概念,但是根据传入参数的不同,js的函数可以完成不同的功能,也可说是 ...

  2. jquery 中一些 特殊方法 的特殊使用 一览表

    cnblogs的页面, 一种是管理页面, 是随笔的列表 a full list of essays. 另一种是 首页. 要搜索文档的话, 就使用 "首页"的那种方式. 一个jque ...

  3. 用JQuery中的Ajax方法获取web service等后台程序中的方法

    用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...

  4. jquery中的ajax方法参数

    引用来自:http://www.cnblogs.com/tylerdonet/p/3520862.html jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String ...

  5. jquery-12 jquery中的工具方法有哪些

    jquery-12 jquery中的工具方法有哪些 一.总结 一句话总结:四个较常用方法.1.isArray();2.isFunction();3.isEmptyObejct();4.trim(); ...

  6. 解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy”错的方法

    解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the ...

  7. jQuery中的join方法

    和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...

  8. jQuery中的ready方法及实现按需加载css,js

    模拟jQuery中的ready方法及实现按需加载css,js 一.ready函数的实现 经常用jQuery类库或其他类库中的ready方法,有时候想想它们到底是怎么实现的,但是看了一下jQuery中的 ...

  9. $.ajax()方法详解 jquery中的ajax方法

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

随机推荐

  1. How to Convert a Date Time to “X minutes ago” in C# z

    http://www.codeproject.com/Articles/770323/How-to-Convert-a-Date-Time-to-X-minutes-ago-in-Csh In one ...

  2. Zabbix探索:Discovery任务、进程以及占用率

    刚刚又报错了,如下所示: Zabbix discoverer processes more than 75% busy 原因是,我配置了一个自动发现的任务.而每个自动发现的任务都会在一定时间内占用一个 ...

  3. <转>DNS SOA记录

    http://www.sigma.me/2011/01/01/about_dns_soa.html 今天登入google webmaster,发现有好多crawl错误,一看,都是Domain name ...

  4. Codeforces Round #355 (Div. 2)

    A 弯腰 #include<cstdio> #include<cstring> #include<iostream> #include<queue> # ...

  5. Solution for Latex error: "Cannot determine size of graphic"

    I'm trying to include graphics in my Latex-file, which I compiled with latex+dvipdf on OS X. Latex h ...

  6. berserkJS(大名:狂暴JS / 昵称:疯子JS)

    极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 http://www.finalshares.com/read-6763?f=g-20

  7. Delphi XE5 常用功具与下载

    1.Delphi XE5 正式版 http://altd.embarcadero.com/download/radstudio/xe5/delphicbuilder_xe5_win.iso http: ...

  8. 转载 : Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结 投稿:jingxian 字体:[增加 减小] 类型:转载 时间:2013-11-14我要评论 本文是 ...

  9. C# 通用数据访问类(SqlHelper)

    [转]C# 通用数据访问类(SqlHelper) 注:本文转自http://www.tzwhx.com/newOperate/html/3/31/312/13080.htmlVisual C# 动态操 ...

  10. oracle的commit

    oracle的commit就是提交数据(释放锁),在未提交前你前面的操作更新的都是内存,没有更新到物理文件中.执行commit从用户角度讲就是更新到物理文件了,事实上commit时还没有写date f ...