简体   繁体   English

jQuery slideUp()影响其他列表的slideDown()

[英]jQuery slideUp() effected for other list's slideDown()

I am facing a problem. 我面临一个问题。 I have some list items. 我有一些清单项目。 When a list is clicked, it's own child will be open and an '.open' class will be added. 单击列表后,将打开其自己的子级,并添加一个'.open'类。 When clicking on that '.open' class, that list's child will be hidden. 单击该'.open'类时,该列表的子级将被隐藏。 So, I made my code like this: 因此,我将代码编写如下:

$('body').on('click', '.list-group li a', function() {
    // hide all other child
    $('.list-group .child').hide();
    $('.list-group li a').removeClass('open');

    // Open this list's child
    $(this).addClass('open');
    $(this).parent().find('.child').slideDown();
});

$('body').on('click', '.list-group li a.open', function() {    
    // hide this list's child
    $(this).removeClass('open');
    $(this).parent().find('.child').slideUp();
});

But, at the time of hiding the $(this) list's child, it will try to slideDown() at first and then slideUp() ! 但是,在隐藏$(this)列表的子级时,它将首先尝试slideDown()然后再slideUp() I can understand that this code might be responsible for this: 我可以理解此代码可能对此负责:

// hide all other child
$('.list-group .child').hide();
$('.list-group li a').removeClass('open');

How to fix this? 如何解决这个问题?

Fiddle Work 小提琴作品

It looks like your click listeners are firing both times when .open is applied. .open应用.open时,您的click监听器似乎都两次触发。 Using a :not selector in your .on('click', '.list-group li a') function seems to solve this .on('click', '.list-group li a')函数中使用:not选择器似乎可以解决此问题

$('body').on('click', '.list-group li a:not(.open)', function() {
    // ....
}

JSFiddle Link JSFiddle链接

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

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