Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

jquery - 获取当前悬停的li相对于可见li的索引-jQuery hover(get index of current hovered li respect to visible li only - jquery hover)

There are 5 li element and one of them say third li is hidden.Is there a way to apply filter before getting index so that fourth element's index should show 2 instead of 3.

(有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>
  ask by kiranking translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about using the visible selector :

(如何使用可见选择器 :)

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

EDIT:

(编辑:)

Too bad, you could try an alternative approach if that is an option for you:

(太糟糕了,如果您可以选择以下方法,则可以尝试其他方法:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...