JQuery編寫自己的外掛程式(七)

來源:互聯網
上載者:User

標籤:

一:jQuery外掛程式的編寫基礎
1、外掛程式的種類
編寫外掛程式的目的是給一系列已經方法或函數做一個封裝,以便在其他地方重複使用,方便後期維護和提高開發效率。
常見的種類有以下三種:
封裝對象方法的外掛程式

二:編寫自己的外掛程式
1、封裝jQuery對象方法的外掛程式
Step1:架構
;(
    function($) {
    $.fn.extend({
        ….
        });
    }
)(jQuery)
 

執行個體:改變背景色

;(    function($) {        $.fn.extend({            "color": function(value) {                return this.css("color", value);            },            "bgcolor": function(value) {                return this.css("background-color", value);            }        });    })(jQuery)

 調用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>    <script src="Scripts/jquery-1.2.6-vsdoc-cn.js" type="text/javascript"></script>    <script src="Scripts/jquery.color.js" type="text/javascript"></script>    <script type="text/javascript">        $(document).ready(function() {        $("div").click(function() {                $(this).bgcolor(this.innerText);            });        });    </script></head><body><div>red</div><div>blue</div><div>green</div></body></html>

 

2.編寫全域函數外掛程式

Step1:寫一個封裝全域函數的外掛程式;(    function($) {    $.extend({        ….        });    })(jQuery)代碼:去除空格
;(    function($) {        $.extend({            ltrim: function(text) {                return text.replace(/^\s+/g, "");            },            rtrim: function(text) {                return text.replace(/\s+$/g, "");            }        });    })(jQuery)

調用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>    <script src="Scripts/jquery-1.2.6-vsdoc-cn.js" type="text/javascript"></script>    <script src="Scripts/jquery.trim.js" type="text/javascript"></script>    <script type="text/javascript">        $(document).ready(function() {            var teststr = "      test   ";            alert(teststr.length);            var s1 = $.ltrim(teststr);            alert(s1.length);            var s2 = $.rtrim(teststr);            alert(s2.length);        });    </script></head><body></body></html>

 3.編寫自已的外掛程式-MyModalForm
Step1:寫一個封裝全域函數的外掛程式
;(
    function($) {
    $.extend({
        ….
        });
    }
)(jQuery)  

 

效果:

代碼:

;(    function($) {        var divW; //DIV寬度        var divH; //DIV高度        var clientH; //瀏覽器高度        var clientW; //瀏覽器寬度        var divTitle; //DIV標題        var pageUrl; //DIV中載入的頁面        var div_X; //DIV橫座標        var div_Y; //DIV縱座標        $.extend({            //清除背景鎖定            clearLockScreen: function() {                $("#divLock").remove();            },            //清除DIV視窗            clearDivWindow: function() {                $("#divWindow").remove();            },            //返回彈出的DIV的座標            divOpen: function() {                var minTop = 80; //彈出的DIV記頂部的最小距離                if ($("#divWindow").length == 0) {                    clientH = $(window).height(); //瀏覽器高度                    clientW = $(window).width(); //瀏覽器寬度                    div_X = (clientW - divW) / 2; //DIV橫座標                    div_Y = (clientH - divH) / 2; //DIV縱座標                    div_X += window.document.documentElement.scrollLeft; //DIV顯示的實際橫座標                    div_Y += window.document.documentElement.scrollTop; //DIV顯示的實際縱座標                    if (div_Y < minTop) {                        div_Y = minTop;                    }                    $("body").append("<div id=‘divWindow‘><div id=‘divTitle‘><img src=‘images/Close-1.gif‘ id=‘x‘ /></div><div id=‘divContent‘>載入中...</div></div>"); //增加DIV                    //divWindow的樣式                    $("#divWindow").css("position", "absolute");                    $("#divWindow").css("z-index", "200");                    $("#divWindow").css("left", (div_X + "px")); //定位DIV的橫座標                    $("#divWindow").css("top", (div_Y + "px")); //定位DIV的縱座標                    $("#divWindow").css("opacity", "0.9");                    $("#divWindow").width(divW);                    $("#divWindow").height(divH);                    $("#divWindow").css("background-color", "#FFFFFF");                    $("#divWindow").css("border", "solid 1px #333333");                    //divTitle的樣式                    $("#divTitle").css("height", "20px");                    $("#divTitle").css("line-height", "20px");                    $("#divTitle").css("background-color", "#333333");                    $("#divTitle").css("padding", "3px 5px 1px 5px");                    $("#divTitle").css("color", "#FFFFFF");                    $("#divTitle").css("font-weight", "bold");                    //x的樣式                    $("#x").css("float", "right");                    $("#x").css("cursor", "pointer");                    //divContent的樣式                    $("#divContent").css("padding", "10px");                }                else {                    clientH = $(window).height(); //瀏覽器高度                    clientW = $(window).width(); //瀏覽器寬度                    div_X = (clientW - divW) / 2; //DIV橫座標                    div_Y = (clientH - divH) / 2; //DIV縱座標                    div_X += window.document.documentElement.scrollLeft; //DIV顯示的實際橫座標                    div_Y += window.document.documentElement.scrollTop; //DIV顯示的實際縱座標                    if (div_Y < minTop) {                        div_Y = minTop;                    }                    $("#divWindow").css("left", (div_X + "px")); //定位DIV的橫座標                    $("#divWindow").css("top", (div_Y + "px")); //定位DIV的縱座標                }            },            //鎖定背景螢幕            lockScreen: function() {                if ($("#divLock").length == 0) {//判斷DIV是否存在                    clientH = $(window).height(); //瀏覽器高度                    clientW = $(window).width(); //瀏覽器寬度                    $("body").append("<div id=‘divLock‘></div>")//增加DIV                    $("#divLock").height(clientH);                    $("#divLock").width(clientW);                    $("#divLock").css("display", "block");                    $("#divLock").css("background-color", "#000000");                    $("#divLock").css("position", "fixed");                    $("#divLock").css("z-index", "100");                    $("#divLock").css("top", "0px");                    $("#divLock").css("left", "0px");                    $("#divLock").css("opacity", "0.5");                }                else {                    clientH = $(window).height(); //瀏覽器高度                    clientW = $(window).width(); //瀏覽器寬度                    $("#divLock").height(clientH);                    $("#divLock").width(clientW);                }            },            ShowModalForm: function(divWidth, divHeight, title, url) {                divW = divWidth; //DIV寬度                divH = divHeight; //DIV高度                divTitle = title; //DIV高度                pageUrl = url; //DIV中載入的頁面UR                $.lockScreen(); //鎖定背景                $.divOpen();                $("#divTitle").append(divTitle);                $("#divContent").load(pageUrl, function() {                    //加上拖動的效果                    $.AttachDrag("divWindow");                });                //交換X圖片                $("#x").hover(                            function() {                                $(this).attr("src", "images/Close-2.gif");                            },                            function() {                                $(this).attr("src", "images/Close-1.gif");                            }                            );                //關閉DIV視窗                $("#x").click(                            function() {                                $.clearDivWindow();                                $.clearLockScreen();                            });                //視窗大小改變時                $(window).resize(                                    function() {                                        if ($("#divLock").length != 0) {                                            $.lockScreen();                                        }                                        if ($("#divWindow").length != 0) {                                            $.divOpen();                                        }                                    }                                    );            }        });    })(jQuery)

 

 調用的代碼:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo3.aspx.vb" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>封裝全域函數的外掛程式Demo</title>    <link href="CSS/Main.css" rel="stylesheet" type="text/css" />    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>   <%-- <script src="Scripts/jquery-1.2.6-vsdoc-cn.js" type="text/javascript"></script>--%>
//拖動的js
  <script src="Scripts/jquery.modalform.js" type="text/javascript"></script>
//彈出表單 注意引用的順序
   <script src="Scripts/jquery.modalform.js" type="text/javascript"></script>    <script type="text/javascript">        $(document).ready(function() {        $("a").click(function() {            $.ShowModalForm(500, 320, ‘封裝全域函數的外掛程式Demo‘, ‘GetStudentInfo.aspx‘);            });        });    </script></head><body><a href="javascript:void(0)">點此示範</a></body></html>

 CSS樣式:

@charset "utf-8";* {margin: 0px;padding: 0px;}/* download by http://www.codefans.net */body {margin: 50px;font-family: Arial, "宋體";font-size: 12px;}

 

四:隔行變色:

效果:

 

外掛程式代碼:

jquery.alterBgColor.js

; (function($) {    $.fn.extend({        "alterBgColor": function(options) {            options = $.extend({                odd: "odd", //偶數行的樣式                even: "even", //奇數行的樣式                selected: "selected" //選中行的樣式            }, options);            $("tbody>tr:odd", this).addClass(options.odd);            $("tbody>tr:even", this).addClass(options.even);            $("tbody>tr", this).click(function() {                //判斷當前是否選中                var hasSelected = $(this).hasClass(options.selected);                //如果已經有了selected樣式,則移除,否則則添加上selected樣式                //$(this)[hasSelected ? "removeClass" : "addClass"](options.selected);                if (hasSelected)                    $(this).removeClass(options.selected).find(":checkbox").attr("checked", false);                else                    $(this).addClass(options.selected).find("checkbox").attr("checked", true);            });            //如果有行checkbox選中的,預設情況下,讓背景高亮度            $("tbody>tr:has(:checked)", this).addClass(options.selected);                        return this;  //返回一個jquery 對象,讓此方法可鏈        }    });})(jQuery);

 調用的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><style type="text/css">table{ border:0;border-collapse:collapse;}td{ font:normal 12px/17px Arial;padding:2px;width:100px;}th{ font:bold 12px/17px Arial;text-align:left;padding:4px;border-bottom:1pxsolid #333;}.even{ background:#FFF38F;}  /* 偶數行樣式*/.odd{ background:#FFFFEE;}  /* 奇數行樣式*/.selected{ background:#FF6500;color:#fff;}</style><!--   引入jQuery -->    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>    <script src="Scripts/jquery.alterBgColor.js" type="text/javascript"></script><script type="text/javascript">//外掛程式應用$(function(){$("#table2").alterBgColor()  //應用外掛程式.find("th").css("color","red");//可以鏈式操作})</script></head><body><table id="table1"><thead><tr><th> </th><th>姓名</th><th>性別</th><th>暫住地</th></tr></thead><tbody><tr><td><input type="checkbox" name="choice" value=""/></td><td>張山</td><td>男</td><td>浙江寧波</td></tr><tr><td><input type="checkbox" name="choice" value="" /></td><td>李四</td><td>女</td><td>浙江杭州</td></tr><tr><td><input type="checkbox" name="choice" value="" checked="checked" /></td><td>王五</td><td>男</td><td>湖南長沙</td></tr><tr><td><input type="checkbox" name="choice" value="" /></td><td>找六</td><td>男</td><td>浙江溫州</td></tr><tr><td><input type="checkbox" name="choice" value="" /></td><td>Rain</td><td>男</td><td>浙江杭州</td></tr><tr><td><input type="checkbox" name="choice" value="" checked="checked" /></td><td>MAXMAN</td><td>女</td><td>浙江杭州</td></tr></tbody></table><br /><br /><table id="table2"><thead><tr><th> </th><th>姓名</th><th>性別</th><th>暫住地</th></tr></thead><tbody><tr><td><input type="checkbox" name="choice" value=""/></td><td>張山</td><td>男</td><td>浙江寧波</td></tr><tr><td><input type="checkbox" name="choice" value="" /></td><td>李四</td><td>女</td><td>浙江杭州</td></tr><tr><td><input type="checkbox" name="choice" value="" checked="checked" /></td><td>王五</td><td>男</td><td>湖南長沙</td></tr><tr><td><input type="checkbox" name="choice" value="" /></td><td>找六</td><td>男</td><td>浙江溫州</td></tr><tr><td><input type="checkbox" name="choice" value="" /></td><td>Rain</td><td>男</td><td>浙江杭州</td></tr><tr><td><input type="checkbox" name="choice" value="" checked="checked" /></td><td>MAXMAN</td><td>女</td><td>浙江杭州</td></tr></tbody></table></body></html>

 五:滑鼠拖動:myDragLibrary.js

var MyDragHandler = {    DragPanelId: "divContext",    _idiffx: 0,    _idiffy: 0,    _Div: null,    AttachDrag: function(dragId) {        if (dragId)            MyDragHandler._Div = document.getElementById(dragId);        else            MyDragHandler._Div = document.getElementById(MyDragHandler.DragPanelId);        document.body.onmousedown = MyDragHandler._handleMouseDown;    },    _handleMouseDown: function() {        var oEvent = window.event;        if (MyDragHandler._Div) {            MyDragHandler._idiffx = oEvent.clientX - MyDragHandler._Div.offsetLeft;            MyDragHandler._idiffy = oEvent.clientY - MyDragHandler._Div.offsetTop;            document.body.onmousemove = MyDragHandler._handleMouseMove;            document.body.onmouseup = MyDragHandler._handleMouseUp;        }    },    _handleMouseMove: function() {        var oEvent = window.event;        MyDragHandler._Div.style.left = oEvent.clientX - MyDragHandler._idiffx;        MyDragHandler._Div.style.top = oEvent.clientY - MyDragHandler._idiffy;        MyDragHandler._Div.style.cursor = "move";    },    _handleMouseUp: function() {        document.body.onmousemove = null;        document.body.onmouseup = null;        MyDragHandler._Div.style.cursor = "default";    }}

 調用的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>      <style type="text/css">            #div1 {                background-color: red;                height: 100px;                width: 100px;                position: absolute;            }        </style>    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>    <script src="Scripts/jquery.myDragLibrary.js" type="text/javascript"></script>    <script type="text/javascript">        $(function() {                        $.AttachDrag("div1");        });    </script></head><body><div id="div1"></div> </body></html>

 

 

 

 

JQuery編寫自己的外掛程式(七)

聯繫我們

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