修改函数以在正确的位置应用类

时间:2017-11-13 05:05:30

标签: jquery

使用以下功能:

function calculateLIsInRow() {
  var lisInRow = 0;
  $('.mobileversionworkflow ul li span:first-child').each(function() {
    $(this).removeClass("back");
    if ($(this).prev().length > 0) {
      if ($(this).position().top != $(this).prev().position().top) {
        $('.mobileversionworkflow ul li span:first-child').prev().addClass("back");
        //return false;
      }
      lisInRow++;
    } else {
      lisInRow++;
    }
    if ($(this).next().length > 0) {} else {
      $(this).addClass("back");
    }
  });
}
calculateLIsInRow();

这是问题,li将类作为“返回”,我希望将类应用于类border_process之后的border_process back

<li class="back"> <span class="border_process"></span>
                                 <span class="process_workflow_box">
                                 <span class="process_workflow_box_im">
                                 <img src="images/c2.png" alt="">
                                 </span>
                                 <span class="process_workflow_txt">
                                 discovery <br>&amp; research
                                 </span>
                                 </span>
                              </li> 

我希望将其应用于以下

<li> <span class="border_process back"></span>
                                     <span class="process_workflow_box">
                                     <span class="process_workflow_box_im">
                                     <img src="images/c2.png" alt="">
                                     </span>
                                     <span class="process_workflow_txt">
                                     discovery <br>&amp; research
                                     </span>
                                     </span>
                                  </li>

1 个答案:

答案 0 :(得分:0)

function calculateLIsInRow() {
  var lisInRow = 0;
  $('li span:first-child').each(function() {
    $(this).addClass("back");
    if ($(this).prev().length > 0) {
      if ($(this).position().top != $(this).prev().position().top) {
        $('li span:first-child').prev().addClass("back");
        //return false;
      }
      lisInRow++;
    } else {
      lisInRow++;
    }
    if ($(this).next().length > 0) {} else {
      $(this).addClass("back");
    }
  });
}
calculateLIsInRow();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="back"> <span class="border_process"></span>
                                 <span class="process_workflow_box">
                                 <span class="process_workflow_box_im">
                                 <img src="images/c2.png" alt="">
                                 </span>
                                 <span class="process_workflow_txt">
                                 discovery <br>&amp; research
                                 </span>
                                 </span>
                              </li>

似乎我能够将类添加回跨度。希望它有所帮助