jQuery的toggle()的自動觸發真煩人

來源:互聯網
上載者:User

標籤:

jQuery的toggle()方法應該是在滑鼠點擊後才會觸發,現在的問題是在ready載入後就自動觸發了,怎麼回事呢?

答案是jQuery的版本問題,在1.9以後的版本toggle()就存在這個問題,用之前的版本就正常了。?

如果你非常喜歡這個功能,有2個辦法,一個辦法是jquery官網提供的一個版本升級檔案。http://blog.jquery.com/2013/05/08/jquery-migrate-1-2-1-released/

另外一個辦法是笨辦法,就是把原來的那塊代碼拿出來,寫成一個外掛程式。

<!doctype html><html><head>    <meta charset="UTF-8">    <title>test</title>    <script type="text/javascript" src="http://cdn.staticfile.org/jquery/1.11.1/jquery.js"></script></head><body><button>test click me</button><script>    $(document).ready(function(){        $("button").toggle(function(){            $("body").css("background-color", "green");        }, function(){            $("body").css("background-color", "red");        }, function(){            $("body").css("background-color", "yellow");        });    });    $.fn.toggle = function( fn ) {        // Save reference to arguments for access in closure        var args = arguments,                guid = fn.guid || jQuery.guid++,                i = 0,                toggler = function( event ) {                    // Figure out which function to execute                    var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;                    jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );                    // Make sure that clicks stop                    event.preventDefault();                    // and execute the function                    return args[ lastToggle ].apply( this, arguments ) || false;                };        // link all the functions, so any of them can unbind this click handler        toggler.guid = guid;        while ( i < args.length ) {            args[ i++ ].guid = guid;        }        return this.click( toggler );    }</script></body></html>

 

jQuery的toggle()的自動觸發真煩人

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.