【问题标题】:Jquery - Get div number from a list by hoveringJquery - 通过悬停从列表中获取 div 编号
【发布时间】:2011-08-19 12:18:01
【问题描述】:

如何通过将鼠标悬停在 div 上来获取数字?

HTML

<div id="log"></div>

<div id="myList">

    <div class="divHV">One </div>
    <div class="divHV">Two</div>
    <div class="divHV">3 </div>

    <div class="divHV">Blub </div>
    <div class="divHV">Peter</div>
    <div class="divHV">6 House</div>

    <div class="divHV">last one is 7</div>    

</div>

JS

$(document).ready(function() {

    $('.divHV').hover(function()
    {
        XX = '';
        $('#log').html('This is the div number '+XX+' <br />');

    }, function ()
    {
        $('#log').html('');
    });     

});

工作示例
http://jsfiddle.net/9R4aC/2/

【问题讨论】:

    标签: javascript jquery list count hover


    【解决方案1】:
    XX = $(this).index();
    

    但这将从 0 开始,如果你愿意,你可以添加 +1..

    http://jsfiddle.net/9R4aC/2/

    【讨论】:

    • 就是这样!非常感谢!
    【解决方案2】:
    $(document).ready(function() {
    
        $('.divHV').each(function(i) {
            $(this).hover(function() {
                XX = i;
                $('#log').html('This is the div number ' + XX + ' <br />');
    
            }, function() {
                $('#log').html('');
            });
    
        });
    
    });
    

    http://jsfiddle.net/9R4aC/3/

    【讨论】:

    • 我认为 Vytautas 的解决方案要快得多。感谢您的快速答复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多