繁体   English   中英

如何减少这种 jQuery?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM