我用Javascript/jQuery创建了一个带有我自己的自定义步骤的滑块.
在这里,我使用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);
Run Code Online (Sandbox Code Playgroud)
第二个代码(做了工作): - 用awhile
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);
Run Code Online (Sandbox Code Playgroud)
这不是一个真正的问题,但我仍然想知道为什么第一个不起作用.
任何人都可以告诉我为什么我必须使用一个while循环?
Poi*_*nty 10
在第一个中,你将两次递增"i":一次在循环体的末尾,一次在循环标题中(i++在括号中).
当你添加while循环时,你基本上使for循环(大多数)无关紧要,因为当while循环退出时,"i"的值将导致for循环也退出.
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |