jQuery插件写法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yingzhicai/article/details/51547282

1.在jQuery匿名函数中,采用jQuery.extend();方法创建jQuery插件

2.在jQuery匿名函数中,采用对象.属性=函数的方式创建jQuery插件

<html>
    <head>
        <title>最简单的jquery插件</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="../res/jquery/jquery-1.4.4.min.js"></script>
        <script type="text/javascript">
            (function($) {
                jQuery.extend({//写法1
                    a: function(h){
                        $("#ad").html(h);
                    },
                    b:function(h){
                        alert(h);
                    }
                })
            })(jQuery);

            (function($) {//写法2
                jQuery.a=function(h){
                    $("#ad").html(h);
                }
                jQuery.b=function(h){
                    alert(h);
                }
            })(jQuery);

            $(document).ready(function(){
                $.a("abc");
                $.b("xyz");
            });

        </script>

    </head>
    <body>
        <h3>最简单的jQuery插件</h3>
        <div id="ad"></div>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/yingzhicai/article/details/51547282