0

所以在我的Javascript中,我有这个

$('ul li').click(function(){ //loops through all <li>'s inside a <ul>

    $('ul .clicked').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
    $(this).addClass('clicked'); // add .clicked class to the clicked <li> ($(this))

    $(this).screenSlide();
});

现在,screenSlide 函数是这样的

$.fn.screenSlide = function(){ // $(this) = aboutSectorNineteen (<li>'s id)

    var test = $('.current').attr('id'); //if an element with .current as it's class exists, let test be equal to that elements id
    test = "#" + test;
    $(test).slideUp(); // slide it up, hide it and remove the .current class from the <li> element
    $(test).hide();
    $(test).removeClass('current');
    var gettingShown = $(this).attr('id');
    gettingShown = "#" + gettingShown + "Slide";
    $(gettingShown).addClass('current'); // add the .current class to $(this) <li>
    $(gettingShown).slideDown();
};

现在,gettingShown 确实向上滑动,当我单击另一个 <li> 时,向上滑动的屏幕 (gettingShown) 确实隐藏了,但它没有向上滑动。意思就是

$(test).hide();

但是正在工作

$(test).slideUp();

不工作,对吧?这是为什么?我也尝试将 slideUp 更改为 fadeOut 但这仍然没有奏效。我将 slideDown 更改为 fadeIn 并且它起作用了。为什么 slideUp 和 fadeOut 不起作用?我使用不正确吗?

4

2 回答 2

1

slideUp()是异步的,它在完成向上滑动时隐藏元素。

它应该是

$(test).slideUp(function () {
    $(this).removeClass('current');
});
于 2013-10-10T03:51:09.497 回答
1

这是绑定事件和操作的更简洁版本。

$('ul > li').click(function() { //loops through all <li>'s inside a <ul>
    $('li').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
    $(this).addClass('clicked').screenSlide(); // add .clicked class to the clicked <li> ($(this))
});

$.fn.screenSlide = function() { // $(this) = aboutSectorNineteen (<li>'s id)
    var test = $('.current').attr('id'); //if an element with .current as it's class exists, let test be equal to that elements id
    $('#' + test).slideUp().removeClass('current'); // slide it up, hide it and remove the .current class from the <li> element
};
于 2013-10-10T03:53:40.997 回答