zoukankan      html  css  js  c++  java
  • 解决jQuery的toggle()的自动触发问题

    在1.9以后的版本toggle()就存在这个问题,用之前的版本就正常了

    解决办法:

    1.去 jquery官网下载一个版本升级文件。http://blog.jquery.com/2013/05/08/jquery-migrate-1-2-1-released/

    2.把原来的那块代码拿出来,写成一个插件

     
    <!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>

  • 相关阅读:
    ORACLE的自定义排序
    PDA上一个UDP通讯客户端
    构造
    sort()的使用
    VC2012链接mySql
    宏的一些代码
    试卷
    疑问
    代码1
    vector学习
  • 原文地址:https://www.cnblogs.com/weimingxin/p/8663356.html
Copyright © 2011-2022 走看看