jQuery .each() index?

后端 未结 5 2005
盖世英雄少女心
盖世英雄少女心 2021-01-31 01:03

I am using

$(\'#list option\').each(function(){
//do stuff
});

to loop over the options in a list. I am wondering how I could get the index of

5条回答
  •  -上瘾入骨i
    2021-01-31 01:41

    jQuery takes care of this for you. The first argument to your .each() callback function is the index of the current iteration of the loop. The second being the current matched DOM element So:

    $('#list option').each(function(index, element){
      alert("Iteration: " + index)
    });
    

提交回复
热议问题