开发者

Jquery - Get div number from a list by hovering

开发者 https://www.devze.com 2023-03-29 20:30 出处:网络
How can a get the number by hovering mouse over the div? HTML <div id=\"log\"></div> <div id=\"myList\">

How can a get the number by hovering mouse over the 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"开发者_StackOverflow中文版>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('');
    });     

});

Working exmaple

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


XX = $(this).index();

but that will start from 0 you can add +1 if you want..

http://jsfiddle.net/9R4aC/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/

0

精彩评论

暂无评论...
验证码 换一张
取 消