admin管理员组

文章数量:1279083

How do I do the following pseudo code in 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
        }
    });  
});

How do I do the following pseudo code in 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
        }
    });  
});
Share Improve this question edited Apr 23, 2010 at 5:47 Timj asked Apr 23, 2010 at 5:42 TimjTimj 14.2k3 gold badges19 silver badges9 bronze badges 2
  • $(this) is referring to $(".btn-slide") which is not toggled.... am i lost??? – Reigel Gallarde Commented Apr 23, 2010 at 5:55
  • I believe you might be having a problem with the fact that it is an animation and possibly still in progress when you get to the isToggled part of the code. – SeanJA Commented Apr 23, 2010 at 5:58
Add a ment  | 

4 Answers 4

Reset to default 4

Or you could just use the toggle function: http://api.jquery./toggle/

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

Note: You can have as many toggles as you want, just add more functions:

$('#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.');
});

[EDIT]

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

Try using a callback function for 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
            }
        });
    });            
 });  
$(document).ready(function(){
    $(".btn-slide").click(function(){
        if ('#divToSlide').is(':visible')) {
            // do something when toggled
        } else {
            // do something different when not toggled
        }
    });  
});

try:

$(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();
        }
    );  
});

本文标签: javascriptJQuery How do I determine if a slideToggle has been toggledStack Overflow