Pass dynamic id value jquery div jump to function

前端 未结 4 1155
忘了有多久
忘了有多久 2021-01-16 14:31

I am stuck at jquery div jump to problem. The problem is that i am creating dynamic and dynamic div also say <

4条回答
  •  攒了一身酷
    2021-01-16 14:55

    Visualize it here

    First, since you have multiple links, use a class to group them:

    HTML

    Click me 1_1
    Click me 1_2    
    Click me 1_3
    

    jQuery

    $(document).on('click', '.click', function (e) {
        var theID = $(this).attr('id');
        $('html, body').animate({
            scrollTop: $('#' + theID + '_div').offset().top
        }, 1000);
        return false;
    });
    

    I did this with the slight assumption you were dynamically creating these links (hence the delegation). If they are static and won't change during page load, you can use $('.click').click(function()... instead of $(document).on('click', '.click', function()...

提交回复
热议问题