Toggle next element with jQuery

前端 未结 4 1944
难免孤独
难免孤独 2021-01-20 16:58

I have a problem with the this element (I know how this is working).

I have a lot of that html structure. When I click on the a button, the

相关标签:
4条回答
  • 2021-01-20 17:28

    Description

    You should use jQuery.parent() and jQuery.next() to get this done. Check out the sample and this jSFiddle Demonstration.

    Sample

    Html

    <p class="actions">
        <a href="#" class="button" title="Toevoegen"><span>Toevoegen</span></a>
    </p>
    <div class="extra-options-tickets" style="display: none; ">
    text
    </div>
    
    <p class="actions">
        <a href="#" class="button" title="Toevoegen"><span>Toevoegen</span></a>
    </p>
    <div class="extra-options-tickets" style="display: none; ">
    text
    </div>
    

    jQuery

    $(".button").click(function () {
        var div = $(this).parent().next();
        if(div.is(":visible")) {
            div.hide();
        } else {
            div.fadeIn(450);
        };
    });
    

    More Information

    • jSFiddle Demonstration
    • jQuery.next()
    • jQuery.parent()
    0 讨论(0)
  • 2021-01-20 17:36

    Change your code so that you get the extra options div directly after the clicked link like this:

    buttonSubmit.click(function (e) {
        e.preventDefault();
        // Get the extra options directly after the clicked link
        var extraOptions = $(this).closest('p.actions').next('div.extra-options-tickets');
    
        if(extraOptions.is(":visible")) {
            extraOptions.hide();
        } else {
            extraOptions.fadeIn(450);
        };
    
    });
    
    0 讨论(0)
  • 2021-01-20 17:40

    The this element represents the element that invoked the action witch in your case, it's the <a> element.

    and it seams you want to only show, the next <div class="extra-options-tickets"> and not all of them, so you need to think like this:

    In this code, if what fires the click is the <a> (your selector), how do I go to reach the <div> I want to show?

    <p class="actions">
        <a href="#" class="button" title="Toevoegen"><span>Toevoegen</span></a>
    </p>
    <div class="extra-options-tickets" style="display: none; ">text</div>
    

    From <a> you can navigate to the previous element, witch is the <p class="actions"> and the next element (the <div>) is the one I want...

    translating this to code:

    var extraOptions = $(this) // my <a> element
                         .prev() // previous element (<p class="actions">)
                         .next(); // next element from <p> is <div class="extra-options-tickets">
    

    you can always make more generic so you can safely add more elements in between, for example:

    instead of calling the .prev() (previous element), find the closest elemnt with a class of actions and from there, find the next element down the DOM with a class of extra-options-tickets, translating:

    var extraOptions = $(this).closest(".actions").next(".extra-options-tickets");
    
    0 讨论(0)
  • 2021-01-20 17:41

    see this example I made for you: http://jsfiddle.net/manuel/PmNwh/

    I use the jquery next() function to get the first .extra-options-tickets

    0 讨论(0)
提交回复
热议问题