如何找出jQuery中每个()的最后一个索引? [英] How to Find Out Last Index of each() in jQuery?
本文介绍了如何找出jQuery中每个()的最后一个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类似的东西......
I have something like this...
$( 'ul li' ).each( function( index ) {
$( this ).append( ',' );
} );
我需要知道最后一个元素的索引是什么,所以我可以这样做......
I need to know what index will be for last element, so I can do like this...
if ( index !== lastIndex ) {
$( this ).append( ',' );
} else {
$( this ).append( ';' );
}
任何想法,伙计们?
推荐答案
var total = $('ul li').length;
$('ul li').each(function(index) {
if (index === total - 1) {
// this is the last one
}
});
这篇关于如何找出jQuery中每个()的最后一个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文