使用jQuery从H3向下滑动下一个UL

Slide Down Next UL from H3 with jQuery

本文关键字:下一个 UL jQuery H3 使用      更新时间:2023-09-26
<h3>Details<span class="dropdown">View</span></h3>
    <div id="sbc">
        <ul>
            <li><h2>Role: Supervisor</h2></li>
            <li>Manager Contact:</li>
            <li class="small">*location closed - please contact corporate office for further assistance</li>
            <li><a href=""></a></li>
            <li><h2>Responsibilities</li>
            <li>
                <ul>
                <li>Assist customers with miscellaneous issues from AIV, AppStore, Kindle Tier 2</li>
                <li> - Fallback - Kindle Tier 1</li>
                </ul>
            </li>
        </ul>
    </div>

尝试编写jQuery来onClick滑下UL下面的跨度。尝试了多种解决方案,但似乎都失败了。

这是我的jQUery代码:
<script>
(function() {
    $('span.dropdown').on('click', function() {
        $(this).next('ul').slideToggle();
    });
})(jQuery);
</script>

绞尽脑汁....我不是一个完全的新手(但显然我是),希望得到一些帮助。

点击

* * *滑下来:ul:跨度后

切换。

您需要这样做:

$(this)
    .parent()        // Go to the parent h3 element of current span clicked
    .next('#sbc')    // Go to the next div element with id 'sbc'
    .children('ul')  // Go to its child ul element
    .slideToggle();  // and finally slide toggle

回答了自己的问题....(是的,12秒后)

<script>
(function() {
    $('span.dropdown').on('click', function() {
        $(this).parent().next('ul').slideToggle();
    });
})(jQuery);
</script>

谢谢你的帮助!我知道我是个白痴,错误地记录了DOM:-P

您需要先遍历到父元素,获得下一个元素,然后获得子元素ul:

$(this).parent().next().children('ul').slideToggle();

因为你的div有一个id,并且id必须是唯一的,你可以这样做:

$('#sbc > ul').slideToggle();

试试这个

$('span.dropdown').on('click', function() {
    $('ul').slideToggle();
   // alert('hi');
});

试试这个让我知道

$('.dropdown').on('click', function() {
       $(this).parent().next().children('ul').slideToggle();
 });