gpt4 book ai didi

jquery - 检查列表中的元素是否为第一个

转载 作者:行者123 更新时间:2023-12-01 03:12:32 25 4
gpt4 key购买 nike

我有一个列表,并且我正在使用 jQuery 来删除列表对象。

$(document).off('click', '#delete').on('click', '#delete',function(e) {
$(this).parent().remove();
});

我想知道如何判断要删除的元素是否是列表中的第一个元素?

最佳答案

您还可以使用.index()位置。该函数返回一个基于零的索引,因此,对于第一个元素,您将得到 0。另一个有趣的点是,在 javascript 中,0 是 false,所有其他数字都是 true 。如果该元素是第一个元素,则 .index() 函数返回 false

$(document).off('click', '#delete').on('click', '#delete',function(e) {
if(!$(this).parent().index()){
//the element is the first element
}
});

This is the full DOC for the .index() function.

关于jquery - 检查列表中的元素是否为第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24369055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com