admin管理员组文章数量:1305354
I'm wanting to add a class 'slide-number-x' to an element within each slide in a slider, with the 'x' representing the slide number in the dom.
So, for example this simple HTML markup:
<div class="slider">
<div class="slide"><div class="slide-title"></div></div>
<div class="slide"><div class="slide-title"></div></div>
<div class="slide"><div class="slide-title"></div></div>
<div class="slide"><div class="slide-title"></div></div>
</div>
Would bee:
<div class="slider">
<div class="slide slide-number-1"><div class="slide-title"></div></div>
<div class="slide slide-number-2"><div class="slide-title"></div></div>
<div class="slide slide-number-3"><div class="slide-title"></div></div>
<div class="slide slide-number-4"><div class="slide-title"></div></div>
</div>
I thought this would work:
$('.slide').each(function(i){
$(this).addClass('slide-number'i+1);
});
Any help or ideas?
Many thanks, R
I'm wanting to add a class 'slide-number-x' to an element within each slide in a slider, with the 'x' representing the slide number in the dom.
So, for example this simple HTML markup:
<div class="slider">
<div class="slide"><div class="slide-title"></div></div>
<div class="slide"><div class="slide-title"></div></div>
<div class="slide"><div class="slide-title"></div></div>
<div class="slide"><div class="slide-title"></div></div>
</div>
Would bee:
<div class="slider">
<div class="slide slide-number-1"><div class="slide-title"></div></div>
<div class="slide slide-number-2"><div class="slide-title"></div></div>
<div class="slide slide-number-3"><div class="slide-title"></div></div>
<div class="slide slide-number-4"><div class="slide-title"></div></div>
</div>
I thought this would work:
$('.slide').each(function(i){
$(this).addClass('slide-number'i+1);
});
Any help or ideas?
Many thanks, R
Share Improve this question asked Jan 23, 2013 at 20:19 John the PainterJohn the Painter 2,6158 gold badges64 silver badges105 bronze badges5 Answers
Reset to default 9You forgot the concatenation operator (+
), use:
$('.slide').each(function(i){
$(this).addClass('slide-number-' + (i+1));
});
It looks like it should be:
$(this).addClass('slide-number-' + (i + 1));
This gives you what you want
$('.slide').each(function(i){
$(this).addClass('slide-number-' + (i+1));
});
You're + is in the wrong place... Try this. It worked for me...
$('.slide').each(function(i){
$(this).addClass('slide-number'+i);
});
You are missing the concatenation operator. Also, the convention for incrementing a value in JavaScript is the the double plus.
$('.slide').each(function(i){
$(this).addClass('slide-number-' + ++i);
});
本文标签: javascriptAdd class to div with number increasing for each divStack Overflow
版权声明:本文标题:javascript - Add class to div with number increasing for each div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741804349a2398409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论