[英]How can I reduce this kind of jQuery?
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');
}
});
你可以只 select an.active 元素
jQuery('li.active')
所以你只需要
jQuery('li.active').prevAll().addClass('finished');
您能解释一下为什么需要 each() 循环吗?
jQuery('.msnf-form-step').each(function() {
//
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.