檢查列表中的元素是否是第一個

[英]Check whether element in a list is the first


I have a list and I'm using jQuery to allow for list objects to be removed.

我有一個列表,我正在使用jQuery來刪除列表對象。

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

I was wondering how do I tell if the element being deleted is the first on the list?

我想知道如何判斷被刪除的元素是否是列表中的第一個?

3 个解决方案

#1


2  

You can also use the .index() position. This function returns a zero based index so, for the first element, you get 0. Another interesting point is that 0, in javascript, is false and all other numbers are true. The .index() function, then, returns false if the element is the first element.

您也可以使用.index()位置。這個函數返回一個從零開始的索引,因此,對於第一個元素,你得到0.另一個有趣的觀點是,在javascript中,0是假的,所有其他數字都是真的。如果元素是第一個元素,則.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.

這是.index()函數的完整DOC。

#2


1  

You could use is and :first

你可以使用is和:first

So it would be something like:

所以它會是這樣的:

if($(this).parent().is(':first')) {
    // this was the first element
}

#3


1  

You can use the is method:

你可以使用is方法:

if ($(this).parent().is(':first-child'))
{
}

See Documentation

見文檔


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/06/23/72081cd2b81307e3eec66eef3319bcb0.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com