开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜