【问题标题】:Replacement for .toggle( in jQuery 1.9 [closed]替换 .toggle( 在 jQuery 1.9 [关闭]
【发布时间】:2014-01-22 15:26:11
【问题描述】:

我在此处为 jQuery 1.9 找到了不同的切换选项,但我无法在此处使用它:

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

【问题讨论】:

标签: jquery click toggle


【解决方案1】:

你可以给 .flip 一个数据属性

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

或者你可以使用 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】:

    您可以使用“click”事件,然后使用“:visible”进行检查(如果元素此时可见):

    • 如果元素可见 - 通过添加类似代码的类来隐藏它,或者只是:~.hide()

    • 如果元素不可见 - 添加/删除类或使用:~.show()

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 2014-08-14
      • 2013-03-31
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多