我有以下循环:
for(var myScreen in wizardScreens){
if(step==index)$(myScreen).show();
else $(myScreen).hide();
index++;
}
wizardScreens定义为$(“.wizardScreen”,wizard);,其中wizard是一个DOM元素.在循环中,myScreen设置为字符串,而不是DOM元素.任何人都可以解释为什么会这样吗?
解决方法:
jQuery集合已经有了内置的迭代函数:
wizardscreens.each(function (index, screen) {
if (index == step)
$(screen).show();
else
$(screen).hide();
}
或者甚至可能更适合您的使用:
var activescreen = wizardscreens.eq(step);
activescreen.show();
wizardscreens.not( activescreen[0] ).hide();
这完全避免了显式迭代.