How to get the eq of visible list in jQuery?

How can I get the index of the list that is visible in jQuery and store it in a variable? So for the instance slideshow below:

  • I create a variable currSlides.
  • I store the index of the visible li into currSlides
  • currSlides should output as 3.
  • I will use that number to manipulate the slides.

    <ul class="slides-container">
      <li style="display:none">Image 1</li>
      <li style="display:none">Image 2</li>
      <li>Image 3</li>
      <li style="display:none">Image 4</li>
    </ul>
    

  • 尝试这个:

    var currSlides  = $('.slides-container li').index($('.slides-container li:visible'));
    

    var currSlides  = $('.slides-container li').index($('.slides-container li:visible'));
    
    alert(currSlides);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul class="slides-container">
      <li style="display:none">Image 1</li>
      <li style="display:none">Image 2</li>
      <li>Image 3</li>
      <li style="display:none">Image 4</li>
    </ul>

    This should work.

    var currSlides  = $('.slides-container').find('li:visible');
    

    And also this

    var currSlides = $('.slides-container li:visible');
    

    Demo

    链接地址: http://www.djcxy.com/p/83544.html

    上一篇: 在属性magento中添加自定义属性

    下一篇: 如何获得jQuery中可见列表的eq?