jquery 2.1.4 切换问题

jquery 2.1.4 toggle issue

我在我的项目中使用 Jquery 1.7。所以最近我升级到 jquery 2.1.4.

下面的代码现在无法使用,但在较早的旧版本中它可以使用。 2.1.4 支持切换,请告诉我下面的代码有什么问题。

 jQuery('#show-hide-filter-text').toggle(function(){
            jQuery(this).text(hideTxt);
            jQuery('#filterListDiv').slideDown("medium");
        }, function(){
            jQuery(this).text(showTxt);
            jQuery('#filterListDiv').slideUp("medium");
        });

toggle 的形式是 removed in jQuery 1.9。您将其替换为一个简单的 click 处理程序:

jQuery('#show-hide-filter-text').on("click", function() {
    var $this = jQuery(this),
        toggled = !!$this.data("toggled");
    if (toggled) {
        $this.text(showTxt);
        jQuery('#filterListDiv').slideUp("medium");
    } else {
        $this.text(hideTxt);
        jQuery('#filterListDiv').slideDown("medium");
    }
    $this.data("toggled", !toggled);
});

根据 the docs,通常使用持续时间参数和回调函数(而不是您提供的 2 个函数)调用 toggle 方法。

可行的替代方案可能类似于以下内容

var hidden = false;
jQuery('#show-hide-filter-text').toggle(
  "medium", 
  function() {
    if (!hidden) {
      jQuery(this).text(hideTxt);
      jQuery('#filterListDiv').slideDown("medium");
    } else {
      jQuery(this).text(showTxt);
      jQuery('#filterListDiv').slideUp("medium");
    }
  }
);

发生这种情况是因为您使用的 toggle() 方法是事件处理套件的一部分,并且在 1.8 版中已弃用。 您可以改用效果套件中的 toggle() 方法。区别在于传递的参数集。