Как я могу уменьшить этот вид jQuery? - PullRequest
0 голосов
/ 24 октября 2019
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');
  }
});

1 Ответ

0 голосов
/ 24 октября 2019

Вы можете просто выбрать .active элемент

jQuery('li.active')

Так что вам нужно только

jQuery('li.active').prevAll().addClass('finished');

Не могли бы вы объяснить, почему вам нужен каждый цикл ()?

jQuery('.msnf-form-step').each(function() {
    //
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...