JQuery:如何确定是否已切换slideToggle?

时间:2010-04-23 05:42:15

标签: javascript jquery

如何在JQuery中执行以下伪代码?

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#divToSlide").slideToggle("fast");
        if ($(this).isToggled) {  // <---- DOESN'T WORK -------
            // do something when toggled
        } else {
            // do something different when not toggled
        }
    });  
});

4 个答案:

答案 0 :(得分:4)

或者您可以使用切换功能:http://api.jquery.com/toggle/

$('#target').toggle(function() {
    alert('First handler for .toggle() called.');
}, function() {
    alert('Second handler for .toggle() called.');
});

注意:您可以根据需要进行多次切换,只需添加更多功能:

$('#target').toggle(function() {
    alert('1 handler for .toggle() called.');
}, function() {
    alert('2 handler for .toggle() called.');
}, function() {
    alert('3 handler for .toggle() called.');
}, function() {
    alert('4 handler for .toggle() called.');
}, function() {
    alert('5 handler for .toggle() called.');
});

<强> [编辑]

$('a.toggleButton').toggle(function() {
    $("#divToSlide").slideDown("fast");
    // do something when toggled
}, function() {
    $("#divToSlide").slideUp("fast");
    // do something when toggled    });
});

答案 1 :(得分:4)

尝试使用slideToggle的回调函数

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#divToSlide").slideToggle("fast", function(){
            if ($(this).is(":visible")) {  // <---- DOESN'T WORK -------
            // do something when toggled
            }
            else {
            // do something different when not toggled
            }
        });
    });            
 });  

答案 2 :(得分:1)

$(document).ready(function(){
    $(".btn-slide").click(function(){
        if ('#divToSlide').is(':visible')) {
            // do something when toggled
        } else {
            // do something different when not toggled
        }
    });  
});

答案 3 :(得分:1)

尝试:

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#divToSlide").toggle(
        function() {  
            // do something when toggled
            $(this).slideUp();
        },
        function(){
            // do something different when not toggled
            $(this).slideDown();
        }
    );  
});