jquery counting elements
I wondering if it is possible to count the number of elements using jque开发者_StackOverflow中文版ry and then using that prefix the element with a number, so for example I have 2 selects on a page, is it possible to count the selects and then give the selects, a class e.g.,
<select class="1">
<select class="2">
Hmm, something like:
$("select").each( function (i) {
$(this).addClass( "select_" + i ); //Classes can't start with a number.
});
$("select").each(function(i, sel) {
$(sel).addClass("class-" + i);
}
Edit: forgot classes can't start with a number.
$('select').each(function(index) {
$(this).addClass('prefix_' + (index + 1));
});
Note that a class name cannot start with a number so make sure the class name starts with a letter and then put the index.
精彩评论