简体   繁体   English

JQuery切换时只点击上一个跨度显示

[英]JQuery toggle on click only previous span display

Trying to display only the .active-box span previous to each .filter. 尝试仅显示每个.filter之前的.active-box范围。 I've tried using .prev, .closest, .siblings, and a couple other methods but I can't seem to get anything to work. 我尝试过使用.prev,.closest,.siblings和其他一些方法,但我似乎无法使用任何东西。 Depending on what I use it will either toggle all or none, can't seem to single out the .active-box. 根据我使用它,它将切换所有或没有,似乎无法挑出.active-box。 I know some basic JQuery but I'm stumped on this and running out of ideas. 我知道一些基本的JQuery,但我对此感到困惑并且没有想法。 What am I doing wrong? 我究竟做错了什么?

I've also tried each method with the spans inside the <li> tag. 我也尝试了<li>标签内的每个方法。

.empty-box displays an empty box and .active-box fills the empty box with a smaller square on click. .empty-box显示一个空框,.active-box填充空框,点击一个较小的方框。

HTML: HTML:

                <div class="filter-dropdown">
                    <ul>
                        <span class="empty-box"><span class="active-box"></span></span><li class="filter" class="active" data-filter="all" data-dimension="season">All</li>
                        <span class="empty-box"><span class="active-box" style="display: none;"></span></span><li class="filter" data-filter="winter" data-dimension="season">Winter</li>
                        <span class="empty-box"><span class="active-box" style="display: none;"></span></span><li class="filter" data-filter="fall" data-dimension="season">Fall</li>
                        <span class="empty-box"><span class="active-box" style="display: none;"></span></span><li class="filter" data-filter="summer" data-dimension="season">Summer</li>
                        <span class="empty-box"><span class="active-box" style="display: none;"></span></span><li class="filter" data-filter="spring" data-dimension="season">Spring</li>
                    </ul>
                </div>

JQuery: JQuery的:

    $('.filter').click(function() {
        $('span').closest('.active-box').toggle();});

You need: 你需要:

$('.filter').click(function() {
    $(this).prev('.empty-box').find('.active-box').toggle();
});

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

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