0

我在这里有这段代码,我想用 jquery 做的是获取从 1 开始的列表项总数,并找到哪个索引slideshow_currentView类在其中。

例如,我有 38 个项目并且slideshow_currentView在项目 7 上,我想显示 7 / 38,我对 jquery 完全陌生,只是想知道这是否可能?

<div class="slideshow_pagination" style="display: block;">
<div class="slideshow_pagination_center">
<ul>
<li class="slideshow_transparent slideshow_currentView">
  <span style="display: none;">0</span>
</li>
<li class="slideshow_transparent "><span style="display: none;">1</span></li>
<li class="slideshow_transparent "><span style="display: none;">2</span></li>
<li class="slideshow_transparent "><span style="display: none;">3</span></li>
<li class="slideshow_transparent "><span style="display: none;">4</span></li>
<li class="slideshow_transparent "><span style="display: none;">5</span></li>
<li class="slideshow_transparent "><span style="display: none;">6</span></li>
<li class="slideshow_transparent "><span style="display: none;">7</span></li>
<li class="slideshow_transparent "><span style="display: none;">8</span></li>
<li class="slideshow_transparent "><span style="display: none;">9</span></li>
<li class="slideshow_transparent "><span style="display: none;">10</span></li>
<li class="slideshow_transparent "><span style="display: none;">11</span></li>
<li class="slideshow_transparent "><span style="display: none;">12</span></li>
<li class="slideshow_transparent "><span style="display: none;">13</span></li>
<li class="slideshow_transparent "><span style="display: none;">14</span></li>
<li class="slideshow_transparent "><span style="display: none;">15</span></li>
<li class="slideshow_transparent "><span style="display: none;">16</span></li>
<li class="slideshow_transparent "><span style="display: none;">17</span></li>
<li class="slideshow_transparent "><span style="display: none;">18</span></li>
<li class="slideshow_transparent "><span style="display: none;">19</span></li>
<li class="slideshow_transparent "><span style="display: none;">20</span></li>
<li class="slideshow_transparent "><span style="display: none;">21</span></li>
<li class="slideshow_transparent "><span style="display: none;">22</span></li>
<li class="slideshow_transparent "><span style="display: none;">23</span></li>
<li class="slideshow_transparent "><span style="display: none;">24</span></li>
<li class="slideshow_transparent "><span style="display: none;">25</span></li>
<li class="slideshow_transparent "><span style="display: none;">26</span></li>
<li class="slideshow_transparent "><span style="display: none;">27</span></li>
<li class="slideshow_transparent "><span style="display: none;">28</span></li>
<li class="slideshow_transparent "><span style="display: none;">29</span></li>
<li class="slideshow_transparent "><span style="display: none;">30</span></li>
<li class="slideshow_transparent "><span style="display: none;">31</span></li>
<li class="slideshow_transparent "><span style="display: none;">32</span></li>
<li class="slideshow_transparent "><span style="display: none;">33</span></li>
<li class="slideshow_transparent "><span style="display: none;">34</span></li>
<li class="slideshow_transparent "><span style="display: none;">35</span></li>
<li class="slideshow_transparent "><span style="display: none;">36</span></li>
<li class="slideshow_transparent "><span style="display: none;">37</span></li>
<li class="slideshow_transparent "><span style="display: none;">38</span></li>
</ul>
</div>
</div>
4

2 回答 2

2

试试这个:

var total = 0;
$("li").each(function(){
    var n = parseInt($(this).text());
    if(n > 0){ //check if element is greater than 0
        total ++;
    }
});
var index = $(".slideshow_currentView").index()+1; //.index() gives index, starts from 0
alert("total:"+total+" current:"+index);

在这里拉小提琴。

于 2013-11-12T05:58:53.510 回答
1

试试这个:

$(document).ready(function () {
    $ul = $('.slideshow_pagination_center ul li').hide();
    alert("Length : "+$ul.length);
    $ul.each(function (){
        $(this).show();
        if ($(this).hasClass('slideshow_currentView')){
            alert("Index : "+$(this).find('span').text());
            return false;
        }
    });
});

Demo

显示只有lislideshow_currentView Demo

于 2013-11-12T05:54:39.393 回答