简体   繁体   English

jQuery SlideToggle 不适用于语义上不相邻的元素

[英]jQuery SlideToggle doesn't work on elements that are not semantically adjacent

I'm using the amazing slideToggle();我正在使用惊人的slideToggle(); function from the jQuery API. function 来自 jQuery API。 The issue I'm encountering is that I thought I could target the next .slide_toggle_container by using the following jQuery code:我遇到的问题是我认为我可以使用以下 jQuery 代码来定位下一个.slide_toggle_container

$(document).ready(function() {
    //Hide (Collapse) the toggle containers on load
    $(".toggle_container").hide(); 

    //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
    $(".trigger").click(function(){
        $(this).toggleClass("active").next(".toggle_container").slideToggle("slow");
        return false; //Prevent the browser jump to the link anchor
    });

}); // End of document.ready    

The problem is that the above code works great with the following HTML问题是上面的代码适用于以下 HTML

            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget venenatis turpis. Fusce et ante diam, venenatis vestibulum tortor. Nam ultrices accumsan velit sit amet sagittis.</p>

            <a class="trigger toggle" href="#">Read More</a>

            <div class="toggle_container">Sliding Content</div>

But for styling purposes, I need to use the following HTML, which causes the .trigger and slideToggle to not work.但出于造型目的,我需要使用以下 HTML,这会导致.triggerslideToggle不起作用。

            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget venenatis turpis. Fusce et ante diam, venenatis vestibulum tortor. Nam ultrices accumsan velit sit amet sagittis.<a class="trigger toggle" href="#">Read More</a></p>

            <div class="toggle_container">Sliding Content</div>

My understanding from some research is that the HTML above (2nd) does not work because the .trigger is contained within <p> and the slide_toggle_container is not recognized as the .next() element in the DOM.我从一些研究中了解到,上面的 HTML (第 2 次)不起作用,因为.trigger包含在<p>中,并且slide_toggle_container未被识别为 DOM 中的.next()元素。 Any help?有什么帮助吗? How can I make this work with the second HTML scenario above?如何在上面的第二个 HTML 场景中使用它?

Use parent() method to access the parent of the anchor and then call next to access the div.使用parent()方法访问锚的父级,然后调用 next 访问 div。

Change:改变:

 $(this).toggleClass("active").next(".toggle_container").slideToggle("slow");  

to

 $(this).toggleClass("active").parent().next(".toggle_container").slideToggle("slow"); 

Try this尝试这个

$(document).ready(function() {
    //Hide (Collapse) the toggle containers on load
    $(".toggle_container").hide(); 

    //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
    $(".trigger").click(function(){
        $(this).toggleClass("active").closest("p").next(".toggle_container").slideToggle("slow");
        return false; //Prevent the browser jump to the link anchor
    });

}); // End of document.ready 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM