2025-02-10

 

jQuery給我們寫web應用程序帶來的極大的方便,很多jQuery插件深受用戶喜愛。jQuery插件的特點基本上是用匿名函數的結構書寫,引入jQuery對象作參數。

以下是一個最基本的jQuery插件的實現。知識點:

在jQuery匿名函數中,采用jQuery.extend();方法創建jQuery插件

在jQuery匿名函數中,采用對象.屬性=函數的方式創建jQuery插件

 

<!– 

最簡單的jquery插件 

胡開明 

–> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 

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

        <p id="ad"></p> 

    </body> 

</html>   

 

摘自 wj800的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *