.index() :gt()Selector :lt()Selector

本文介绍了 jQuery 中 index 方法的使用方式及应用场景,包括如何通过 index 方法查找元素的位置、使用选择器定位元素以及区别 .get() 和 .index() 的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Search for given element from among the matched elements.

*This signature does not accept any arguments. --- .index()

*A selector representing a jQuery collection in which to look for an element.  ---.index(selector) 

*The DOM element or first element within the jQuery object to look for. ---.index(element)

If a selector string is passed as an argument,.index() returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector.If the element is not found,.index() will return -1.

The difference between .get()  and  .index()

.get(),which accepts an index and returns a DOM node

.index(),can take a DOM node and returns an index.

<body>
    <p id="h1">H1</p>
    <p id="h2">H2</p>
    <p id="h3">H3</p>
<script src="jquery-3.1.1.js"></script>
<script>
    var listItem = document.getElementById("h3");
    alert("Index: "+$("p").index(listItem));
</script>
Index: 2

Similarly , if we retrieve a jQuery object consisting of one of the three list items,.index() will search for that list item:

<body>
    <p id="h1">H1</p>
    <p id="h2">H2</p>
    <p id="h3">H3</p>
<script src="jquery-3.1.1.js"></script>
<script>
    var listItem = $('#h2');
    alert("Index:"+$("p").index(listItem));
</script>
Index:1

Note that if the jQuery collection used as the .index() method's argument contains more than one element, the first element within the matched set of elements will be used.

<body>
    <p id="h1">H1</p>
    <p id="h2">H2</p>
    <p id="h3">H3</p>
    <p id="h4">H3</p>
<script src="jquery-3.1.1.js"></script>
<script>
    var listItem = $("p:gt(1)");
    alert("Index: "+$("p").index(listItem));
</script>
If we use a string as the .index() method's argument, it is interpreted as a jQuery selector string.The first element among the object's matched elements which also matches this selector is located.

<body>
    <p id="h1">H1</p>
    <p id="h2">H2</p>
    <p id="h3">H3</p>
    <p id="h4">H3</p>
<script src="jquery-3.1.1.js"></script>
<script>
    var listItem = $("#h3");
    alert("Index:"+listItem.index("p"));
</script>
Examples:

<span>Hello World</span>
<p>First P</p>
<p>Second P</p>
<p>Third P</p>
<p>Fourth P</p>
<script src="jquery-3.1.1.js"></script>
<script>
    $("p").click(function () {
        var index =$("p").index(this);
        $("span").text("That was p index #"+index);
    })
</script>


:gt() Selector

Selector all elements at an index greater than index within the matched set.

index: Zero-based index. --- (":gt(index)")

indexFormEnd: Zero-based index. counting backwards from the last element.

:lt() Selector

Select all elements at an index less than index within the matched set.







[2025-07-31 02:11:27] [INFO] 尝试 1/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:30] [INFO] 尝试 2/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:37] [INFO] 尝试 3/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:46] [INFO] 尝试 4/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:59] [INFO] 尝试 5/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:12:14] [ERROR] 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 失败 (重试 5 次) [2025-07-31 02:12:14] [ERROR] 镜像下载失败: quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 [2025-07-31 02:12:14] [WARN] 镜像下载失败: quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 [2025-07-31 02:12:14] [INFO] 使用 crane 直接下载镜像: docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:12:14] [INFO] 尝试 1/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 128.121.146.228:443: connect: connection refused [2025-07-31 02:12:38] [INFO] 尝试 2/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 128.121.146.228:443: connect: connection refused [2025-07-31 02:13:06] [INFO] 尝试 3/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 31.13.87.19:443: connect: connection refused [2025-07-31 02:13:36] [INFO] 尝试 4/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 31.13.87.19:443: connect: connection refused [2025-07-31 02:14:09] [INFO] 尝试 5/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 69.63.178.13:443: connect: connection refused [2025-07-31 02:14:45] [ERROR] 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 失败 (重试 5 次) [2025-07-31 02:14:46] [ERROR] 镜像下载失败: docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] 镜像下载失败: docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] 以下镜像下载失败: [2025-07-31 02:14:46] [WARN] - quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] - docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] 请手动下载或检查网络连接 [2025-07-31 02:14:46] [INFO] ===== 下载系统优化工具 ===== [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: cheat-linux-amd64.gz [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: btop-x86_64-linux-musl.tbz [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: bottom_x86_64-unknown-linux-gnu.tar.gz [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: fd-v8.7.1-x86_64-unknown-linux-gnu.tar.gz [2025-07-31 02:14:46] [INFO] 尝试 1/5: 下载 bat-v0.24.0-x86极-unknown-linux-gnu.tar.gz #=O#- # # curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to ghproxy.com:443 根据错误重新生成完整、有效、可用的下载脚本、离线部署脚本、服务器系统优化脚本,并且包含系统依赖脚本,脚本要求可以重复执行。
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值