在最新的jQuery版本中已弃用.toggle()吗? [英] Has .toggle() been deprecated in the latest jQuery releases?

查看:147
本文介绍了在最新的jQuery版本中已弃用.toggle()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我.toggle()在最新版本中是否已被弃用吗?

Can you tell me if .toggle() has been deprecated in latest releases?

在我的代码中,此方法适用于1.7.2库,但不适用于最新的库.

In my code, this method works with the 1.7.2 library, but doesn't work with the latest.

还有另一种方法吗?

推荐答案

尝试扩展jQuery,定义一个.toggleFn函数

Try extending jQuery , defining a .toggleFn function

(function($) {
  
  jQuery.fn.extend({
    toggleFn: function(fn1, fn2) {
      $.fn.toggleFn.index = fn = $.fn.toggleFn.index || 0;
      try {
        arguments[fn].call(this);
        $.fn.toggleFn.index = !fn ? 1 : void 0;
      } catch (e) {
        console.log(e)
      };
      return this
    }
  });

  jQuery.extend({
    toggleFn: jQuery.fn.toggleFn
  });

}(jQuery));

$("div").on("click", function() {
  $(this).toggleFn(function() {
    $(this).text("click " + 1)
  }, function() {
    $(this).text("click " + 0)
  })
});

div {
  font-size: 36px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>click 0</div>

这篇关于在最新的jQuery版本中已弃用.toggle()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆