问题描述:灵活使用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. linux删除数据文件无备份恢复

    参考 : http://www.lunar2013.com/2013/06/linux-%E8%AF%AF%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6%E6%81%A2%E ...

  2. tap/click on search button on softkeyboard

    driver.sendKeyEvent(84);Appium says it successfully sent in the command but 'search' does not get ta ...

  3. Makefile学习与进阶之Makefile.am和$$(M)的意思

    (1)makefile 中,出现$$(M) 是什么意思,发现还是看实际的Makefile长知识啊 在makefile中,会经常使用shell命令,也经常见到$var 和 $$var的情况,有什么区别呢 ...

  4. bzoj 3130 [Sdoi2013]费用流(二分,最大流)

    Description Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识.    最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量.一个合法的网络 ...

  5. POJ2723-Get Luffy Out(2-SAT)

    题意:有m扇门,每个门上有两把锁,打开任意一个锁都可以打开这扇门.门要按顺序一个一个打开. 现在有n对不同的钥匙,每对钥匙只能用其中一个,问最多能打开多少门. 题解:对钥匙建图,门是限制条件来建边.每 ...

  6. 获取最外层View

    获取最外层View activity.getWindow().getDecorView()

  7. Android实例-如何使用系统剪切板(XE8+小米2)

    结果: 发现个问题,就是粘贴时会清除之前的信息. unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, S ...

  8. A Tour of Go Map literals

    Map literals are like struct literals, but the keys are required. package main import "fmt" ...

  9. nyoj 448 寻找最大数

    寻找最大数 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...

  10. 笔记-动画篇-layout动画初体验

    约束动画的文章要比预计的迟迟来临,最大的原因是没有找到我认为的足够好的动画来讲解约束动画 —— 当然了,这并不是因为约束动画太难.相反,因为约束动画实在太简单了,反而没有足够多的简单动画素材让我选用. ...