Javascript 如何减少这种jQuery?
您可以只选择一个.active元素Javascript 如何减少这种jQuery?,javascript,jquery,Javascript,Jquery,您可以只选择一个.active元素 jQuery('.msnf-form-step').each(function() { if (jQuery('li:nth-child(2)').hasClass('active')) { jQuery('.active').prevAll().addClass('finished'); } if (jQuery('li:nth-child(3)').hasClass('active')) { jQuery('.active').
jQuery('.msnf-form-step').each(function() {
if (jQuery('li:nth-child(2)').hasClass('active')) {
jQuery('.active').prevAll().addClass('finished');
}
if (jQuery('li:nth-child(3)').hasClass('active')) {
jQuery('.active').prevAll().addClass('finished');
}
if (jQuery('li:nth-child(4)').hasClass('active')) {
jQuery('.active').prevAll().addClass('finished');
}
if (jQuery('li:nth-child(5)').hasClass('active')) {
jQuery('.active').prevAll().addClass('finished');
}
if (jQuery('li:last-child').hasClass('active')) {
jQuery('.active').prevAll().addClass('finished');
}
});
所以你只需要
jQuery('li.active')
您能解释一下为什么需要每个()循环吗
既然所有的分支都做完全相同的事情,那么为什么会有
if
s呢?为什么在每个中重复执行此操作?HTML是什么样子的?目标是什么?对不起。我只是个初学者。我不知道。非常感谢你。
jQuery('li.active').prevAll().addClass('finished');
jQuery('.msnf-form-step').each(function() {
//
});