javascript - jquery this.siblings not working correctly - Stack Overflow

I'm currently working on making a timeline for a website page that displays the years and then whe

I'm currently working on making a timeline for a website page that displays the years and then when clicked it should open up and display what happened in those years and keep the other stuff from other years hidden. For some reason my jquery is doing the opposite it keeps what i click on and hides every other year and their data.

Here is my HTML

<div class="timeline">
    <ul>
        <li class="timeli">1996
            <ul class="timeUlSub">
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">test</p>
                </li>
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">test</p>
                </li>
            </ul>
        </li>
        <li class="timeli">1997
            <ul class="timeUlSub">
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">Test</p>
                </li>
            </ul>
        </li>
        <li class="timeli">1999
            <ul class="timeUlSub">
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">test</p>
                </li>
            </ul>
        </li>
    </ul>
</div>

Here is my jquery I guess my problem is that i dont fully understand the .sibling in jquery.

$(document).ready(function(){
    $(".timeli").click(function(){
        $(this).siblings($(".timeUlSub")).slideToggle("slow", function(){});
    });
});

HERE IS THE EDIT FOR ANSWER it works perfectly for me thanks for the help!

$(document).ready(function(){
        $(".timeli").click(function(){
        $(this).find($(".timeUlSub")).slideToggle("slow", function(){});
    });});

I'm currently working on making a timeline for a website page that displays the years and then when clicked it should open up and display what happened in those years and keep the other stuff from other years hidden. For some reason my jquery is doing the opposite it keeps what i click on and hides every other year and their data.

Here is my HTML

<div class="timeline">
    <ul>
        <li class="timeli">1996
            <ul class="timeUlSub">
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">test</p>
                </li>
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">test</p>
                </li>
            </ul>
        </li>
        <li class="timeli">1997
            <ul class="timeUlSub">
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">Test</p>
                </li>
            </ul>
        </li>
        <li class="timeli">1999
            <ul class="timeUlSub">
                <li>
                    <div class="arrow-up"></div>
                    <p class="timeline-description">test</p>
                </li>
            </ul>
        </li>
    </ul>
</div>

Here is my jquery I guess my problem is that i dont fully understand the .sibling in jquery.

$(document).ready(function(){
    $(".timeli").click(function(){
        $(this).siblings($(".timeUlSub")).slideToggle("slow", function(){});
    });
});

HERE IS THE EDIT FOR ANSWER it works perfectly for me thanks for the help!

$(document).ready(function(){
        $(".timeli").click(function(){
        $(this).find($(".timeUlSub")).slideToggle("slow", function(){});
    });});
Share Improve this question edited Apr 28, 2015 at 13:39 Alex Beyer asked Apr 28, 2015 at 13:31 Alex BeyerAlex Beyer 1471 silver badge8 bronze badges 1
  • 3 $(this).siblings($(".timeUlSub")) The $(".timeUlSub") makes no sense. Thinking you want find... – epascarello Commented Apr 28, 2015 at 13:33
Add a ment  | 

4 Answers 4

Reset to default 3

timeUlSub is the child of timeli, and not sibling (same-level elements). Use find() or children()

 $(this).find(".timeUlSub").slideToggle("slow", function(){});

or

 $(this).children(".timeUlSub").slideToggle("slow", function(){});

Note : children() searches for first-level children only, find() searches children, grand-children and so on.

.timeUlSub is child of clicked li element. thus you need to use .find() instead of .siblings().You also do not need the jquery object, you can simply use the selector for required element:

  $(this).find(".timeUlSub").slideToggle("slow", function(){});

Complete Click Event:

$(".timeli").click(function(){
      $(this).find(".timeUlSub").slideToggle("slow", function(){});
});

I believe you want to hide the child ul of the other elements, but also show those of the one that has been clicked...

$(document).ready(function () {
    $(".timeli").click(function () {
        $(this).siblings().find(".timeUlSub").hide("slow");
        $(".timeUlSub", this).show("slow");
    });
});

This behaviour is "open the one I click and hide the rest".

If you want "toggle the one I click", you don't need to worry about siblings at all.

$(".timeUlSub", this).slideToggle("slow");

See it in action on JSFiddle, or the second version.

.timeUlSub is not a sibling, rather a child element of .timeli.

You should refactor your code to:

$(".timeli").click(function(){

    $(this).find(".timeUlSub").slideToggle("slow", function(){});

});

And an even better implementation would be to delegate the click:

$(document).on("click", ".timeli", function(){

    $(this).find(".timeUlSub").slideToggle("slow", function(){});

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365795a4624573.html

相关推荐

  • javascript - jquery this.siblings not working correctly - Stack Overflow

    I'm currently working on making a timeline for a website page that displays the years and then whe

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信