A common function encapsulation based on jquery

Source: Internet
Author: User

<reference path= "Jquery-1.8.0.min.js"/>
/*
* Div or element centered
* @return
*/
JQuery.fn.mCenterDiv = function () {
This.css ("position", "absolute");
This.css ("Border", "1px solid #ccc");
This.css ("Top", ($ (window). Height ()-this.height ())/2 + $ (window). scrolltop () + "px");
This.css ("Left", ($ (window). Width ()-this.width ())/2 + $ (window). ScrollLeft () + "px");
This.show (100);
return this;
};
/*
* Replace all matching characters in the string
* @param asource Source string
* @param afindtext to replace characters
* @param areptext character after replacement
* @return
*/
Jquery.mreplaceall = function (Asource,afindtext, areptext) {
var raregexp = new RegExp (Afindtext, "G");
Return Asource.replace (Raregexp, Areptext);
};
/*
* Determine if object is empty, undefined, or null
* @param Object
* @return
*/
Jquery.misnull = function (obj) {
if (obj = = "" | | typeof (obj) = = "Undefined" | | obj = = null) {
return true;
}
else {
return false;
}
};
/*
* Get URL parameters
* @param name parameter
* @return
*/
Jquery.mgeturlparam = function (name) {
var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
var r = window.location.search.substr (1). Match (REG);
if (r! = null) return unescape (r[2]); return null;
};
/*
* Multiplication function to get the exact multiplication result
* @param arg1 Parameter 1
* @param arg2 Parameter 2
* @return
*/
Jquery.maccmul = function (Arg1, arg2) {
var m = 0, S1 = arg1.tostring (), S2 = arg2.tostring ();
try {m + = S1.split (".") [1].length} catch (e) {}
try {m + = S2.split (".") [1].length} catch (e) {}
Return number (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (ten, M)
}
/*
* Get random numbers
* @param x Lower limit
* @param y Upper Limit
* @return
*/
Jquery.mgetrandom = function (x, y) {
Return parseint (Math.random () * (Y-x + 1) + x);
};
/*
* The value is rounded (Reserved 2 decimal places) after formatting into the amount of the form
* @param num Value (number or string)
* @return Amount format string, such as ' 1,234,567.45 '
*/
jquery.mformatcurrency = function (num) {
num = num.tostring (). Replace (/\$|\,/g, ");
if (IsNaN (num))
num = "0";
Sign = (num = = (num) = Math.Abs (num));
num = Math.floor (num * 100 + 0.50000000001);
cents = num% 100;
num = Math.floor (num/100). toString ();
if (cents < 10)
cents = "0" + cents;
for (var i = 0; i < Math.floor (Num.length-(1 + i)/3); i++)
num = num.substring (0, Num.length-(4 * i + 3)) + ', ' +
Num.substring (Num.length-(4 * i + 3));
Return ((sign)? ': '-') + num + '. ' + cents);
}
/*
* Regular Validation
* @param s validation string
* @param type validation types Money,china,mobile, etc.
* @return
*/
Jquery.mcheck = function (s, type) {
var objbool = false;
var objexp = "";
Switch (type) {
Case ' money '://amount format defined as a positive number with decimals, up to three digits after the decimal point
Objexp = "^[0-9]+[\." [0-9] {0,3}$ ";
Break
Case ' numletter_ '://English letters and numbers and underscore composition
Objexp = "^[0-9a-za-z\_]+$";
Break
Case ' numletter '://English alphabet and number composition
Objexp = "^[0-9a-za-z]+$";
Break
Case ' Numletterchina '://Kanji, alphabet, number composition
Objexp = "^[0-9a-za-z\u4e00-\u9fa5]+$";
Break
Case ' email '://Email address format
Objexp = "^ ([a-za-z0-9_\.\-]) +\@ (([a-za-z0-9\-]) +\.) + ([a-za-z0-9]{2,4}) +$ ";
Break
Case ' Tel '://fixed format
Objexp =/^ ((\ (\d{2,3}\)) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7} (\-\d{1,4})? $/;
Break
Case ' mobile '://Mobile phone number
Objexp = "^ (13[0-9]|15[0-9]|18[0-9]) ([0-9]{8}) $";
Break
Case ' decimal '://floating point
Objexp = "^[0-9]+" ([.] [0-9]+)? $ ";
Break
Case ' URL '://URL
Objexp = "(http://|https://) {0,1}[\w\/\.\?\&\=]+";
Break
Case ' Date '://Date YYYY-MM-DD format
Objexp =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/;
Break
Case ' int '://Integer
Objexp = "^[0-9]*[1-9][0-9]*$";
Break
Case ' int+ '://Positive integer contains 0
Objexp = "^\\d+$";
Break
Case ' int-'://Negative integer contains 0
Objexp = "^ ((-\\d+) | ( 0+)) $ ";
Break
Case ' China '://Chinese
Objexp =/^[\u0391-\uffe5]+$/;
Break
}
var re = new RegExp (OBJEXP);
if (Re.test (s)) {
return true;
}
else {
return false;
}
};
/*
* Gets the value of the control
* @param ControlID Control ID
* @param controltype type such as Text radio
* @return
*/
Jquery.mgetvalue = function (ControlID, ControlType) {
var objvalue = "";
Switch (controltype) {
Case ' text '://Text input box
ObjValue = $.trim ($ ("#" + ControlID + ""). attr ("value")); Take the value to the left and right space
Break
Case ' Radio '://Radio Box
ObjValue = $ ("input[name=" + ControlID + "']:checked"). attr ("value");
Break
Case ' SELECT '://drop-down list
ObjValue = $ ("#" + ControlID + ""). attr ("value");
Break
Case ' checkbox '://Multi Box
$ ("input[name=" + ControlID + "']:checked"). each (function () {
ObjValue + = $ (this). Val () + ",";
});
Break
Default
Break
}
return objvalue;
};
/*
* Set the value of the control
* @param ControlID Control ID
* @param controltype type such as Text radio
* @param controlvalue bound value
* @return
*/
Jquery.msetvalue = function (ControlID, ControlType, Controlvalue) {
Switch (controltype) {
Case ' text '://Text input box
$ ("#txtUserID"). attr ("value", ' This is the binding content '); Populating content
$ ("Input[name= ' radio1 '][value= ' Shanghai ']"). attr ("Checked", true); Radio Group Radio: Set value= ' Shanghai ' as the currently selected item
$ ("#select1"). attr ("value", ' Portugal '); Dropdown box Select: Set value= ' China ' items as the current selection
$ ("Input[name= ' checkbox1 '][value= ' black '],[value= ' Blue ']"). attr ("Checked", true); Multi Box: Set multiple values for the currently selected item
$ ("#" + ControlID + ""). attr ("value", controlvalue); Populating content
Break
Case ' Radio '://Radio Box
$ ("input[name=" + ControlID + "'][value= '" + Controlvalue + "']"). attr ("Checked", true);
Break
Case ' SELECT '://drop-down list
$ ("#" + ControlID + ""). attr ("value", controlvalue);
Break
Case ' checkbox '://Multi Box
$ ("input[name=" + ControlID + "'][value= '" + controlvalue + "'],[value= '" + Controlvalue + "']"). attr ("Checked", true); Multi Box: Set multiple values for the currently selected item
Break
Default
Break
}
};
/*
* Compatible with IE Firefox and other browser automatic jump
* @param URL Jump URLs
* @return
*/
Jquery.mautonav = function (URL) {
if ($.browser.msie) {
var referlink = document.createelement (' a ');
Referlink.href = URL;
Document.body.appendChild (Referlink);
Referlink.click ();
} else {
location.href = URL;
}
};
/*
* Table table parity row set color and move mouse row discoloration
* @param table ID
* @return
*/
Jquery.mtablehover = function (table) {
$ ("#" + table). each (function () {
var o = $ (this);
Set even lines and odd line colors
O.find ("Tr:even"). CSS ("Background-color", "#EFF3FB");
O.find ("Tr:odd"). CSS ("Background-color", "#FFFFFF");
Mouse movement interlaced color hover usage key
O.find ("Tr:not (: First)"). Hover (function () {
$ (this). attr ("Bcolor", $ (this). CSS ("Background-color")). CSS ("Background-color", "#E0E0E0");
}, function () {
$ (this). CSS ("Background-color", $ (this). attr ("Bcolor"));
});
});
};
/*
* GridView Alternate color mouse slide over color multiple selection
* C # Get selection value Request.Form.Get ("Chkitem")
* @param objgridview ID
* @return
*/
Jquery.mgridview = function (Objgridview) {
var headcolor = {background: ' #E0ECFF ', color: ' #333 '};
var normalcolor = {background: ' #f7f6f3 '};
var altercolor = {background: ' #EDF1F8 '};
var hovercolor = {background: ' #89A5D1 '};
var selectcolor = {background: ' #ACBFDF '};
var nullcolor = {};
Get obj ID
var gridviewid = "#" + Objgridview;
Even
$ (Gridviewid + ">tbody tr:even"). CSS (NormalColor);
First
$ (Gridviewid + ">tbody tr:first"). CSS (Nullcolor). CSS (Headcolor);
Odd
$ (Gridviewid + ">tbody tr:odd"). CSS (Altercolor);
Hover
$ (Gridviewid + ">tbody tr"). Click (function () {
var cb = $ (this). Find ("Input:checkbox");
var CHF = typeof (Cb.attr ("checked")) = = = "undefined"? True:false;
Cb.attr ("Checked", CHF);
var expr1 = Gridviewid + ' >tbody >tr >td >input:checkbox:checked ';
var expr2 = Gridviewid + ' >tbody >tr >td >input:checkbox ';
var SelectAll = $ (expr1). Length = = $ (expr2). length;
$ (' #chkAll '). attr (' checked ', selectall);
}). Hover (function () {
$ (this). CSS (Hovercolor);
}, function () {
$ (Gridviewid + ">tbody tr:even"). CSS (NormalColor);
$ (Gridviewid + ">tbody tr:first"). CSS (Nullcolor). CSS (Headcolor);
$ (Gridviewid + ">tbody tr:odd"). CSS (Altercolor);
});
All check
$ ("#chkAll"). Click (function () {
$ (gridviewid + ' >tbody >tr >td >input:checkbox:visible '). attr (' checked ', this.checked);
});
Check Status
$ (gridviewid + ' >tbody >tr >td >input:checkbox '). Click (function () {
var cb = $ (this);
var CHF = typeof (Cb.attr ("checked")) = = = "undefined"? True:false;
Cb.attr ("Checked", CHF);
var expr1 = Gridviewid + ' >tbody >tr >td >input:checkbox:checked ';
var expr2 = Gridviewid + ' >tbody >tr >td >input:checkbox ';
var SelectAll = $ (expr1). Length = = $ (expr2). length;
$ (' #chkAll '). attr (' checked ', selectall);
});
};
/*
* screen is centered to show processing progress
* @param info display text
* @param type 0 Matte 1 No mask
* @param typepic pictures 0:load 1:ok 2:error
* @return
*/
Jquery.mmaskload = function (info, type, typepic) {
var pic = "";
Switch (typepic) {
Case 0://Loading
Pic = "./images/loading.gif";
Break
Case 1://OK
Pic = "./images/right.png";
Break
Case 2://Error
Pic = "./images/error.png";
Break
Default://any other value when
Pic = "./images/loading.gif";
Break
}
if (type = = 0) {
$ ("<div class=\" Datagrid-mask\ "></div>"). CSS (
{
Display: "Block",
Width: "100%",
Position: "Absolute",
Left: "0",
Top: "0",
Opacity: "0.3",
Height: "100%",
Filter: "Alpha (opacity=30)",
Background: "#ccc"
}). AppendTo ("body");
};
$ ("<div class=\" datagrid-mask-msg\ "></div>"). CSS (
{
Position: "Absolute",
Top: "50%",
padding: "12px 5px 10px 30px",
Width: "Auto",
Height: "16px",
border: "1px solid #D1D1D1",
Background: "#ffffff url ('" + pic + "') no-repeat scroll 5px Center",
Display: "Block",
Left: ($ (document.body). Outerwidth (True)-190)/2,
Top: ($ (window). Height ()-45)/2
}). html (info). AppendTo ("body");
};
/*
* Screen Center hides processing progress
* @return
*/
Jquery.mmaskloadclose = function () {
$ (". Datagrid-mask"). Remove ();
$ (". Datagrid-mask-msg"). Remove ();
};
/*
* Create span as tip tip after control
* @param o with this
* @param tip tip Text
* @param typepic pictures 0:load 1:ok 2:error
* @return
*/
Jquery.mtip = function (o, Tip, typepic) {
var pic = "";
Switch (typepic) {
Case 0://Loading
Pic = "./images/loading.gif";
Break
Case 1://OK
Pic = "./images/right.png";
Break
Case 2://Error
Pic = "./images/error.png";
Break
Default://any other value when
Pic = "./images/loading.gif";
Break
}
var etip = document.createelement ("span");
var ObjID = $ (o). attr ("id") + "_tipdiv";
var value = $ (o). Val ();
Absolute path
var x = $ (o). Offset (). Top;
var y = $ (O). Offset (). Left;
var w = $ (o). width ();
var h = $ (o). Height ();
Etip.setattribute ("id", ObjID);
try {
Document.body.appendChild (Etip);
} catch (e) {}
$ ("#" + ObjID). Hide ();
$ ("#" + ObjID). css ({
Top:x,
Left:y + W + 10,
Height:h,
Position: "Absolute"
});
$ ("#" + ObjID). HTML ("$ ("#" + ObjID). Show ();
};
/**
* Ajax Post Submission
* @param URL
* @param param
* @param datat to Html,json,text
* @param callback callback function callback (data)
* @return
*/
Jquery.mjqajax = function (URL, param, Datat, callback) {
$.ajax ({
Type: "Post",
Url:url,
Data:param,
Datatype:datat,
Success:callback,
Error:function () {}
});
};

A common function encapsulation based on jquery

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.