jquery toggle方法

來源:互聯網
上載者:User

標籤:des   http   使用   strong   io   for   cti   ar   

.toggle(function, function, … )

link .toggle(function, function, ... ) removed

This is the "click an element to run the specified functions" signature of .toggle(). It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.

需要注意的是該.toggle()是“綁定兩個或多個處理常式,在點擊時迴圈執行”;另一個.toggle()仍然存在,它是“控制相應組件的顯示和隱藏”;中文晦澀,官方對此二方法的說明如下:

Categories: Deprecated > Deprecated 1.8 | Events > Mouse Events

.toggle(handler(eventObject), handler(eventObject) [,handler(eventObject)])Returns:jQueryversion deprecated: 1.8, removed: 1.9

Description:Bind two or more handlers to the matched elements, to be executed on alternate clicks.

Categories: Effects > Basics

.toggle( [duration ] [, complete ] )Returns:jQuery

Description:Display or hide the matched elements.

這個變化值得注意。對於刪除的這個“.toggle()”方法,官方沒有給出升級措施,但我發現一個方法名和描述都比較相似的方法“.trigger()”,不知道可不可以替代。

我用2.0.0版本執行下面版本:

$(function () {

  $("h5").toggle(function () {
  $(this).next("div").show();
  }, function () {
  $(this).next("div").hide();
  });

});

想用toggle()控制div的單擊顯隱,但是這麼寫為什麼一開啟頁面,標題和div就全漸漸消失了?

 因為。Jquery1.9.1中 toggle(fn1,fn2...)方法 可實現點擊一個元素來執行指定函數。此方法被刪除以減少混亂和提高潛在的模組化程度。只保留了顯示和隱藏的功能!

官網:http://api.jquery.com/toggle/

原始解決方案:全域一個counter=0 click事件中判斷counter%2==0 分別觸發不同的css,然後counter++,簡單粗暴。

或者使用狀態機器,可以看阮一峰寫的文章。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.