【发布时间】:2013-11-22 13:55:26
【问题描述】:
当我使用 jQuery 进行循环时,我真的很困惑如何传递值并重新开始。
我的项目是一个“简单”的推荐旋转器。
<div id="testimonials">
<div class="testimonial">
<p>You're great!</p>
</div>
<div class="testimonial">
<p>Thanks a lot!</p>
</div>
<div class="testimonial">
<p>Rock on!</p>
</div>
</div>
<script>
jQuery(function ($) {
$( document ).ready(function() {
function fade() {
$('.testimonial').hide();
var t = $('.testimonial').length;
alert(t);
for( var i = 1; i <= t; i++){
function slideit(){
var i = i;
$('.testimonial.nth-child(' + i + ')').show('slow');
$('.testimonial.nth-child(' + i + ')').hide('slow').delay(2000);
}
slideit();
}
}
fade();
});
});
</script>
现在,我被困住了。克服困难的指针?
【问题讨论】:
-
$('.testimonial.nth-child[' + i + ']')应该是$('.testimonial:nth-child(' + i + ')') -
感谢@Shikiryu。完成!
-
注意,如果你只是在寻找一个优雅的 jQuery 脚本来解决这个功能,请参阅stackoverflow.com/a/8616480/497438