Replacement for .toggle( in jQuery 1.9 [closed]

2019-03-06 18:24发布

问题:

I found different alternatives to toggle here for jQuery 1.9, but I don't get it to work im my case here:

$('.thumb.flip').toggle(
function () {
$(this).find('.thumb-wrapper').addClass('flipStop');
},
function () {
$(this).find('.thumb-wrapper').removeClass('flipStop flipIt');
}
);

回答1:

You can give .flip a data-attribute

<div class="thumb flip" data-clicked="0">

$('.thumb.flip').click(function () {
    var data = $(this).data('clicked'), $descendant=$(this).find('.thumb-wrapper');
    if (data) {
        $descendant.removeClass('flipStop flipIt');
    } else {
        $descendant.addClass('flipStop');
    }
    data == 0 ? $(this).data('clicked', 1) : $(this).data('clicked', 0);
});

Or you can use elseif

$('.thumb.flip').click(function () {
    if ($(this).find('.thumb-wrapper').hasClass('flipstop')) {
        $(this).find('.thumb-wrapper').removeClass('flipStop flipIt');
    } else {
        $(this).find('.thumb-wrapper').addClass('flipStop');
    }
});


回答2:

You can use "click" event and then check using ":visible" (if the element is visible at the moment):

  • if the element is visible - hide it by adding a class like your code or just: ~.hide()

  • if the element isn't visible - add/remove class or use: ~.show()