<分区>
我正在使用 Javascript/jQuery 使用我自己的自定义步骤创建一个 slider 。
这里我使用了一个 for
循环,但不知何故我的函数只有在我将一个 while
循环放入其中时才起作用。
第一个代码(没有用): - 没有while
var steps = '';
// Setting up the steps according to the number of slides
for( var i = 0; i < $itemsCount; ++i ) {
var step = '';
// Find step number and step text
var step_text = $items.eq(i).attr('data-title');
var step_count = i + 1;
// current step will have the class 'current'
var step = i === current ? '<li class="step current"><span data-step="'+ step_count +'">'+ step_text +'</span></li>' : '<li class="step"><span data-step="'+ step_count +'">'+ step_text +'</span></li>';
i++;
steps += step;
}
var navSteps = $( '<ul class="steps"/>' ).append(steps).prependTo($slider);
第二个代码(有效): - 使用while
var steps = '';
// Setting up the steps according to the number of slides
for( var i = 0; i < $itemsCount; ++i ) {
var step = '';
// Find step number and step text
while (i < $itemsCount) {
var step_text = $items.eq(i).attr('data-title');
var step_count = i + 1;
// current step will have the class 'current'
var step = i === current ? '<li class="step current"><span data-step="'+ step_count +'">'+ step_text +'</span></li>' : '<li class="step"><span data-step="'+ step_count +'">'+ step_text +'</span></li>';
i++;
steps += step;
}
}
var navSteps = $( '<ul class="steps"/>' ).append(steps).prependTo($slider);
这不是真正的问题,但我仍然想知道为什么第一个不起作用。
谁能告诉我为什么我必须使用 while
而不是循环?