Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何减少这种jQuery?_Javascript_Jquery - Fatal编程技术网

Javascript 如何减少这种jQuery?

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').

您可以只选择一个.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() {
    //
});