The method of encapsulating jquery's classes and plugins into SEAJS modules

Source: Internet
Author: User

This article mainly introduces the jquery class, plug-in encapsulation into the Seajs module method, the need for friends can refer to the next

Note: The SEAJS version used in this article is 2.1.1

A module that encapsulates jquery into Seajs

Define (function () {///Place the jquery code here to put your favorite jquery version in the return $.noconflict ();});

Call Method:
This way, you can use jquery as before

Define (function (Require, exports, module) {var $ = require ('./js/jquery ');/$ (document). Ready (function () {//$ ("tr"). W Yhinterlaced ({"Odd": "Red", "even": "Blue"});//$ ("tr"). Wyhhover ();/})});

Second, the class of jquery encapsulated into SEAJS module

Define (function (Require, exports, module) {var $ = require (": /js/jquery ") var weekday = new Array (7) weekday[0] =" Monday "; weekday[1] =" Tuesday "; weekday[2] =" Wednesday "; weekday[3] =" Thursday "; weekday [4] = "Friday"; weekday[5] = "Saturday"; weekday[6] = "Sunday"; function GetType (ARG) {var today = new Date (); var year = Today.getfullye AR (); var month = Today.getmonth () + 1;var td = Today.getdate (); var d = weekday[today.getday ()-1];var h = today.gethours ( var m = today.getminutes (); var s = today.getseconds (); switch (ARG) {Case 1://2013-09-09 09:31:56return year + "-" + Mon th + "-" + TD + "+ H +": "+ M +": "+ S; Break;case 2://2013-09-09 (Monday) 09:31:56return year + "-" + month + "-" + TD + "(" + D + ")" + H + ":" + M + ":" + S; Break;case 3://09-09-2013 09:31:56return month + "-" + TD + "-" + year + "" + H + ":" + M + ":" + S; Break;case 4://09-09-2013 Monday 09:31:56return month + "-" + TD + "-" + Year + "(" + D + ")" + H + ":" + M + ":" + S; Break;case 5://September 09, 2013 09:31 sec 56return year + "years" + MontH + "Month" + TD + "Day" + H + "when" + M + "min" + S + "SEC";  Break;case 6://September 09, 2013 Monday 09:31 seconds 56return year + "years" + month + "month" + TD + "Day (" + D + ")" + H + "when" + M + "min" + S + "Seconds"; break;}};/ /* Function name: gettime/* parameter: Numeric value (including shaping floating-point type can be, floating-point type will do rounding processing, if not the number, The function will return with the default time format! The time style has 15 "1-15 is a valid time style that exceeds or is less than all will be in the default style style 1")/* feature to get the current system time customizable format **************************************************** /function GetTime (ARG) {if (!isnan (ARG)) {var num = Math.Round (ARG), if (Num < 7 && num > 0) {return GetTy PE (num);} else {var str;var str2;switch (num) {case 0:return GetType (1); Break;case 7:str = GetType (2); return Str.replace (/week/, " "); Break;case 8:str = GetType (1); Return Str.replace (/-/, "/"). Replace (/-/, "/"); Break;case 9:str = GetType (2); Return Str.replace (/-/, "/"). Replace (/-/, "/"); Case 10:str = GetType (2); str2 = str.replace (/-/, "/"). Replace (/-/, "/"); Return Str2.replace (/week/, ""); Break;case 11:str = GetType (4); Return Str.replace (/week)/, ""); Break;case 12:str = GetType (3); Return Str.replace (/-/, "/"). Replace (/-/, "/"); Break;case 13:str = GetType (4); Return Str.replace (/-/, "/"). Replace (/-/, "/"); Case 14:str = GetType (4); str2 = str.replace (/-/, "/"). Replace (/-/, "/"); Return Str2.replace (/week/, ""); Break;case 15:str = GetType (6); Return Str.replace (/week/, "");d Efault:return GetType (1); Break;}}} else {return GetType (1);}};/ * Gets the current number of years of the system */function GetYear () {var today = new Date (); return today.getfullyear ();};/ * Gets the current number of months of the system */function GetMonth () {var today = new Date (), return Today.getmonth () + 1;;};/ * Gets the current number of days of the system */function GetDay () {var today = new Date (); return today.getdate ();};/ * Gets the current hour of the system */function GetHours () {var today = new Date (); return today.gethours ();};/ * Gets the current minute of the system */function Getminute () {var today = new Date (); return today.getminutes ();};/ * Gets the current number of seconds of the system */function Getsecond () {var today = new Date (); return today.getseconds ();};/ Function Name: Timesubmillisecond* parameters: Endtime (end time) starttime (start time) * Function: Get two time millisecond difference, must write a parameter second parameter (start time) can * not write default is the system current time ************************** /function Timesubmillisecond (Endtime, starttime) {var end = new Date (endtime). GetTime (); if (!endtime) {return-1;} if (!starttime) {start = new Date (). GetTime ();} else {start = new Date (starttime). GetTime ();} if (Start > End) {return-1;} else {return end-start;}};/ Function name: timesubnormal* parameter: Endtime (end time) starttime (start time) * Function: Get two time difference value, must write a parameter second parameter (start time) can * not write default is the system current time ************************************************************/ function Timesubnormal (Endtime, starttime) {var end = new Date (endtime). GetTime (); var start;if (!starttime) {start = new D Ate (). GetTime ();} else {start = new Date (starttime). GetTime ();} if (Start > End) {return-1;} else {var alltime = End-start;var seconds = Alltime/1000;var minutes = Math.floor (SECONDS/60); var hours = Math.floor (MINUTES/60); var days = Math. Floor (hours/24); var cday = days;var Chour = hours% 24;var cminute = minutes% 60;var Csecond = math.floor (seconds% 60 var str = "", if (Cday > 0) {str + = Cday + "Day";} if (Chour > 0) {str + = Chour + "Hour";} if (Cminute > 0) {str + = Cminute + "min";} if (Csecond > 0) {str + = Csecond + "SEC";} return str;}; Exports. GetTime = Gettime;exports. GetYear = Getyear;exports. GetMonth = Getmonth;exports. GetDay = Getday;exports. GetHours = Gethours;exports. Getminute = Getminute;exports. Getsecond = Getsecond;exports. Timesubmillisecond = Timesubmillisecond;exports. Timesubnormal = Timesubnormal;})

Call Method:

Define (function (Require, exports, module) {var $ = require ('./js/jquery '); var a=require ('./js/time '); Alert (A.gettime ( 3);});

Third, the jquery plug-in package into the SEAJS module
Here's an example of encapsulating a plugin for jquery into a module

Define (function (require, exports, moudles) {return function (jquery) {(function ($) {//) highlight the current line $.fn.wyhhover = function (op tions) {//options often uses this to indicate that there are many parameters. var defaultval = {BackColor: ' #ccc ',};var obj = $.extend (defaultval, options); return This.each (function () {var tabobject = $ (this); Gets the current object var oldbgcolor = tabobject.css ("Background-color"); Gets the background color of the current object Tabobject.hover (//defines a hover method. function () {tabobject.css ("Background-color", obj. BackColor);},function () {tabobject.css ("Background-color", Oldbgcolor);});}); Make odd and even rows different colors $.fn.wyhinterlaced = function (options) {//options often use this to denote a number of parameters. var defaultval = {odd: ' #DDEDFB ', even: ' #fff ',};var obj = $.extend (defaultval, options); return This.each (function () {var Tabobject = $ (this); Gets the current object if (Tabobject.index ()%2==0) {tabobject.css ("Background-color", obj.odd);} Else{tabobject.css ("Background-color", Obj.even);}}) (jquery);})

Call Method:
Invoking a plug-in using shared methods

Define (function (Require, exports, module) {var $ = require ('./js/jquery '); require ('./js/jquery_tr ') ($);//Share to jquery$ ( Document). Ready (function () {$ ("tr"). wyhinterlaced ({"Odd": "Red", "even": "Blue"}); $ ("tr"). Wyhhover ();})});

The method of encapsulating jquery's classes and plugins into SEAJS modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.