Use JQuery to select parent element of “this” (element clicked)

后端 未结 3 814
傲寒
傲寒 2021-02-07 06:23

I have a jQuery script that creates a carousel to rotate images left and right on user click. At first, I wasnt planning on putting more than one carousel on a single page, but

相关标签:
3条回答
  • 2021-02-07 06:34

    Since your action tags are nested in the first level inside the carousel, you can do this inside each function to know were it belongs :

    var parent = $(this).parent().get(0);
    

    Will actually get you the parent object, which you can now use.

    0 讨论(0)
  • 2021-02-07 06:42

    Inside of an event handler, the variable:

    $(this)

    will get you the calling element. From there, to get the containing div you can use the parent() function:

    $(this).parent()

    Use that to walk through the DOM.

    0 讨论(0)
  • 2021-02-07 06:59

    Use the .parent() function go up a level. Your code might look something like this:

    $('.right-button img').click(function()
        {
            carousel = $(this).parent();
    
            //bunch of code
        }
    
    0 讨论(0)
提交回复
热议问题