jQuery - 按顺序添加类到单独的元素?
我有一些具有不同值的类,我想添加这些类以按顺序设置元素。
这应该只是对我正在寻找的内容的解释:
<div class="These">Lorem ipsum</div>
<div class="These">Lorem ipsum</div>
<div class="These">Lorem ipsum</div>
$('.These').each(function() {
var TheseINDEX = $(this).index();
$(this).addClass('first, second, third');
});
结果:
<div class="These first">Lorem ipsum</div>
<div class="These second">Lorem ipsum</div>
<div class="These third">Lorem ipsum</div>
不像上面那个可憎的东西是完美的,但还有另一件事......我还认为如果它能循环的话那就太酷了。
假设我有 3 个类,就像以前一样,这 3 个类将被添加到 8 个元素中,
结果将是这样的:
<div class="These first">Lorem ipsum</div> <!-- #1 -->
<div class="These second">Lorem ipsum</div>
<div class="These third">Lorem ipsum</div>
<div class="These first">Lorem ipsum</div>
<div class="These second">Lorem ipsum</div>
<div class="These third">Lorem ipsum</div>
<div class="These first">Lorem ipsum</div>
<div class="These second">Lorem ipsum</div> <!-- #8 -->
因为它将继续添加这些预设类,直到没有“.These”可供添加。这个循环的工作原理很像备份,以防你用完类...
http://jsfiddle.net/Z4PAZ/< /a> - 我的例子的 jsfiddle..
有什么想法吗?
I have classes with different values in them and i want to add those classes to set elements in order.
This is supposed to be just an explanation of what im looking for:
<div class="These">Lorem ipsum</div>
<div class="These">Lorem ipsum</div>
<div class="These">Lorem ipsum</div>
$('.These').each(function() {
var TheseINDEX = $(this).index();
$(this).addClass('first, second, third');
});
Result:
<div class="These first">Lorem ipsum</div>
<div class="These second">Lorem ipsum</div>
<div class="These third">Lorem ipsum</div>
Not like that abomination above was perfect but theres another thing.. I'm also thinking that it would be cool if that would loop.
Lets say i have 3 classes just like before and those 3 classes would be added to 8 elements
Result would be this:
<div class="These first">Lorem ipsum</div> <!-- #1 -->
<div class="These second">Lorem ipsum</div>
<div class="These third">Lorem ipsum</div>
<div class="These first">Lorem ipsum</div>
<div class="These second">Lorem ipsum</div>
<div class="These third">Lorem ipsum</div>
<div class="These first">Lorem ipsum</div>
<div class="These second">Lorem ipsum</div> <!-- #8 -->
as in it would keep adding these preset classes until theres no ".These" for them to be added to. This looping would work much like a backup incase you run out of classes...
http://jsfiddle.net/Z4PAZ/ - jsfiddle of my example..
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者,更简单(更高效):
特别是,此版本只需要每个元素调用一次函数,而原始版本则需要三个函数调用(即回调、
$(this)
构造函数和添加类
)。or, more simply (and efficiently):
In particular, this version requires only one function call per element, whereas the original required three (i.e. the callback, the
$(this)
constructor, andaddClass
).http://jsfiddle.net/Z4PAZ/1
http://jsfiddle.net/Z4PAZ/1
如果你将不同的CSS类名存储在一个数组中,你可以使用模运算根据元素的索引得到你想要的类名。此外,each 函数提供了一个参数,该参数是该元素的索引,因此您不需要使用 $(this).index(); 重新计算它
If you store the different CSS class names in an array, you can use modula arithmetic to get the one you want based on the index of the element. Also, the
each
function provides a parameter which is the index of that element so you don't need to recalculate it with$(this).index();