jQuery - Finding next Div

前端 未结 5 1301
逝去的感伤
逝去的感伤 2021-02-13 12:47

I am trying to find the next div after an input button and toggle it.

What am I doing wrong?

JS:

$(\".showNextExperience\").click(function() {
          


        
相关标签:
5条回答
  • 2021-02-13 13:04

    Assuming you have elements between triggering input and manipulated div:

    <input class="showNextExperience" >
    <p>Hello, new line</p>
    <div class="experience">Experience 1</div>
    
    <input class="showNextExperience">
    <p>Some text</p>
    <div>And an info block</div>
    <div class="experience">Experience 2</div>
    

    Then you have to use nextAll to search not only for the very next element but through the whole DOM, which is probably simpler coding than using next().find():

    $(".showNextExperience").click(function() {
        $(this).nextAll(".experience").toggle();
    });
    
    0 讨论(0)
  • 2021-02-13 13:08

    $(this).siblingings().eq(0) should to do it.

    0 讨论(0)
  • 2021-02-13 13:11
    $(".showNextExperience").click(function() {
        $(this).next().show();
    });
    
    0 讨论(0)
  • 2021-02-13 13:14

    Simply:

    $(".showNextExperience").click(function() {
        $(this).next(".experience").toggle();
    });
    

    See:

    • http://api.jquery.com/toggle/
    • http://api.jquery.com/next/
    0 讨论(0)
  • 2021-02-13 13:15

    Try this

    $(".showNextExperience").click(function() {
        $(this).next("div.experience").toggle();
    });
    
    0 讨论(0)
提交回复
热议问题