在最新的jQuery版本中是否已弃用.toggle()?

时间:2015-08-08 22:32:46

标签: jquery

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

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

还有其他办法吗?

3 个答案:

答案 0 :(得分:1)

您可以编写自己的切换功能。它确实被弃用了。这种替换效果很好。

$.fn.toggleClick=function(){
    var functions=arguments, iteration=0
    return this.click(function(){
        functions[iteration].call()
        iteration= (iteration+1) %functions.length
    })
}

用法:

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

请参阅文档:http://api.jquery.com/toggle-event/

答案 1 :(得分:1)

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



(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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

它已被弃用1.8并且在1.9中删除了这里的文档 http://api.jquery.com/toggle-event/