仅获得当前悬停li相对于可见li的索引-jquery悬停

问题描述 投票:0回答:2

[有5个li元素,其中一个说第三个li是隐藏的。有没有一种方法在获取索引之前应用过滤器,以便第四个元素的索引应显示2而不是3。

<html>
    <head>
        <title>test</title>
        <script type='text/javascript' src='js/jquery-1.4.2.js'></script>
        <script>
            $(window).load(function() {
                    jQuery('.addchar').live('hover', function(event) {
                        $("#result").html("Index is:" +$(this).index() );
                    });
                });
            });
        </script>
    </head>
    <body>
        <div id="content">
            <div>
                <ul>
                    <li class="addchar">one </li>
                    <li class="addchar">two </li>
                    <li class="addchar" style="display:none"> three</li>
                    <li class="addchar">four </li>
                    <li class="addchar">five </li>
                </ul>
            </div>
            <div id="result"></div>
        </div>
    </body>
</html>
jquery indexing hover html-lists visible
2个回答
2
投票

如何使用visible selector

$('.addchar:visible').live( ...

编辑:

太可惜了,如果您愿意,您可以尝试一种替代方法:

$(function() {
    $('.addchar:visible').each(function(index) {
        $(this).hover(function() {
            $("#result").html("Index is: " + index);
        });
    });
});

0
投票

作为foreach方法,每个元素的迭代都会随着元素大小的增加而变慢。我进行搜索并使其正常工作。感谢此link

jQuery('.addchar').live('hover', function (event) {
var cInd = $(this).index();//current index including hidden element
var hidLen = $(this).parent().children("li:lt(" + cInd + "):not(:visible)").length;//total hidden before hovered element
var index = cInd - hidLen;
$("#result").html("Current index :" + cInd +"<br>Tot Hidden Before Current:" + hidLen+"<br> Actual Index:"+index);
});
© www.soinside.com 2019 - 2024. All rights reserved.