toggle()隐藏问题的解决方法

一个实例中使用到toggle函数,可是调用的时候会把元素隐藏掉,经搜索终于找到了缘由,须要的朋友能够参考下web

最近编写一个实例的时候使用到toggle函数,可是调用的时候会把元素隐藏掉,以前使用过也只是多个事件轮流切换罢了。百思不得其解因而就在网上搜索查看jQuery API文档。终于发现了缘由: 
原来在jQuery 1.9版本以后,toggle()发生了变化,如下是官网的Notes: 
Note: This method signature was deprecated in jQuery 1.8 and removed in jQuery 1.9. jQuery also provides an animation methodnamed .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of argumentspassed. 
在早期的版本,存在两个同名的toggle(),可是所执行的方法倒是不同的: 
.toggle( handler(eventObject), handler(eventObject) [, handler(eventObject) ] ) 
Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks. 
===================================================== 
.toggle( [duration ] [, complete ] ) 
Description: Display or hide the matched elements. 
而以后的版本把第一个toggle()函数给去掉了,致使用于调用切换功能时会把元素隐藏了。 
======================== 
既然去掉了这个函数,可是实现需求仍是要的。怎么来实现多个事件的轮流切换了? 
能够经过click事件判断不一样的状况来触发,或者经过设置一个变量计数点击次数来执行不一样的函数。ide

var num=0; 
$('#button').click(function(e){ 
    if(num++ %2 == 0){ 
        //doSomething 
    }else{ 
        //doOtherSomething 
    } 
        e.preventDefault(); //阻止元素的默认动做(若是存在) 
});
相关文章
相关标签/搜索