我有一个列表,并且我正在使用 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
}
});
关于jquery - 检查列表中的元素是否为第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24369055/