原來在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事件判斷不同的情況來觸發,或者通過設置一個變量計數點擊次數來執行不同的函數。
var num=0;
$('#button').click(function(e){
if(num++ %2 == 0){
//doSomething
}else{
//doOtherSomething
}
e.preventDefault(); //阻止元素的默認動作(如果存在)
});