toggle()隱藏問(wèn)題的解決方法
更新時(shí)間:2014年02月17日 10:14:38 作者:
一個(gè)實(shí)例中使用到toggle函數(shù),但是調(diào)用的時(shí)候會(huì)把元素隱藏掉,經(jīng)搜索終于找到了原因,需要的朋友可以參考下
最近編寫一個(gè)實(shí)例的時(shí)候使用到toggle函數(shù),但是調(diào)用的時(shí)候會(huì)把元素隱藏掉,之前使用過(guò)也只是多個(gè)事件輪流切換罷了。百思不得其解于是就在網(wǎng)上搜索查看jQuery API文檔。終于發(fā)現(xiàn)了原因:
原來(lái)在jQuery 1.9版本之后,toggle()發(fā)生了變化,以下是官網(wǎng)的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.
在早期的版本,存在兩個(gè)同名的toggle(),但是所執(zhí)行的方法卻是不一樣的:
.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.
而之后的版本把第一個(gè)toggle()函數(shù)給去掉了,導(dǎo)致用于調(diào)用切換功能時(shí)會(huì)把元素隱藏了。
========================
既然去掉了這個(gè)函數(shù),但是實(shí)現(xiàn)需求還是要的。怎么來(lái)實(shí)現(xiàn)多個(gè)事件的輪流切換了?
可以通過(guò)click事件判斷不同的情況來(lái)觸發(fā),或者通過(guò)設(shè)置一個(gè)變量計(jì)數(shù)點(diǎn)擊次數(shù)來(lái)執(zhí)行不同的函數(shù)。
var num=0;
$('#button').click(function(e){
if(num++ %2 == 0){
//doSomething
}else{
//doOtherSomething
}
e.preventDefault(); //阻止元素的默認(rèn)動(dòng)作(如果存在)
});
原來(lái)在jQuery 1.9版本之后,toggle()發(fā)生了變化,以下是官網(wǎng)的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.
在早期的版本,存在兩個(gè)同名的toggle(),但是所執(zhí)行的方法卻是不一樣的:
.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.
而之后的版本把第一個(gè)toggle()函數(shù)給去掉了,導(dǎo)致用于調(diào)用切換功能時(shí)會(huì)把元素隱藏了。
========================
既然去掉了這個(gè)函數(shù),但是實(shí)現(xiàn)需求還是要的。怎么來(lái)實(shí)現(xiàn)多個(gè)事件的輪流切換了?
可以通過(guò)click事件判斷不同的情況來(lái)觸發(fā),或者通過(guò)設(shè)置一個(gè)變量計(jì)數(shù)點(diǎn)擊次數(shù)來(lái)執(zhí)行不同的函數(shù)。
復(fù)制代碼 代碼如下:
var num=0;
$('#button').click(function(e){
if(num++ %2 == 0){
//doSomething
}else{
//doOtherSomething
}
e.preventDefault(); //阻止元素的默認(rèn)動(dòng)作(如果存在)
});
相關(guān)文章
jQuery模擬實(shí)現(xiàn)的select點(diǎn)擊選擇效果【附demo源碼下載】
這篇文章主要介紹了jQuery模擬實(shí)現(xiàn)的select點(diǎn)擊選擇效果,涉及jQuery響應(yīng)鼠標(biāo)點(diǎn)擊動(dòng)態(tài)修改頁(yè)面元素樣式的相關(guān)操作技巧,并附帶demo源碼供讀者下載,需要的朋友可以參考下2016-11-11jQuery實(shí)現(xiàn)滾動(dòng)到底部時(shí)自動(dòng)加載更多的方法示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)滾動(dòng)到底部時(shí)自動(dòng)加載更多的方法,涉及jQuery基于ajax動(dòng)態(tài)操作頁(yè)面元素相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02jQuery.extend 函數(shù)及用法詳細(xì)
Jquery的擴(kuò)展方法extend是我們?cè)趯懖寮倪^(guò)程中常用的方法,該方法有一些重載原型,在此,我們一起通過(guò)本篇文章學(xué)習(xí)jquery.extend函數(shù)及用法詳解吧2015-09-09(模仿京東用戶注冊(cè))用JQuery實(shí)現(xiàn)簡(jiǎn)單表單驗(yàn)證,初學(xué)者必看
下面小編就為初學(xué)者們分享一篇(模仿京東用戶注冊(cè))用JQuery實(shí)現(xiàn)簡(jiǎn)單表單驗(yàn)證,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01