Javascript tool library code

Source: Internet
Author: User

Copy codeThe Code is as follows :/*
* YYJ. js mainly provides background application methods
* Currently, Ajax, addLoadEvent, deleteAll, getByClass, $ C (= getByClass), $, yyjtable, setCss, getCss, and iaArray methods are available. Finally,
* The instantiation method YYJ. tick is used to calculate the script running time.
* Version 0.1 -- tianyi yyj
*/
Var YYJ = function (){
Var UniqueInstance;
Function constrotor (){
Return {
// Ajax Method
Ajax: function (method, url, data, success, fail ){
Var isget = method. toLowerCase () = "get ";
Var xmlhttp = window. XMLHttpRequest? New XMLHttpRequest (): new ActiveXObject ("Microsoft. XMLHTTP ");
Xmlhttp. open (method, url, true );
If (! Isget) xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 ){
If (xmlhttp. status = 200 ){
If (success) success (xmlhttp );
} Else {
If (fail) fail (xmlhttp );
}
}
};
Xmlhttp. send (isget? Null: data );
}, // Ajax
AddLoadEvent: function (func ){
Var oldonload = window. onload;
If (typeof window. onload! = 'Function '){
Window. onload = func;
} Else {
Window. onload = function (){
If (oldonload ){
Oldonload ();
}
Func ();
}
}
}, // AddLoadEvent
DeleteAll: function (checkallbtn, checksomebtn, submitbtn ){
Var checkall = YYJ. $ (checkallbtn );
Var checksome = YYJ. $ (checksomebtn );
Var submit1 = YYJ. $ (submitbtn );
Var checkboxs = (function (){
Var arr = [];
Var check = document. getElementsByTagName ("input ");
For (I = 0; I <check. length; I ++ ){
If (check [I]. getAttribute ("type ")! = "Checkbox ")
Continue;
Arr. push (check [I]);
}
Return arr;
})();
Checkall. onclick = function (){
Checkall. clicked = true;
For (var I = 0; I <checkboxs. length; I ++ ){
If (! Checkboxs [I]. checked ){
Checkall. clicked = false;
Break;
}
}
If (! Checkall. clicked ){
For (var I = 0; I <checkboxs. length; I ++ ){
Checkboxs [I]. checked = true;
}
} Else {
For (var I = 0; I <checkboxs. length; I ++ ){
Checkboxs [I]. checked = false;
}
}
}
Checksome. onclick = function (){
For (var x in checkboxs ){
Checkboxs [x]. checked =! Checkboxs [x]. checked;
}
}
Submit1.onclick = function (){
Var haschecked = false;
Var beSureDel = false;
For (var I = 0; I <checkboxs. length; I ++ ){
If (checkboxs [I]. checked ){
Haschecked = true;
Break;
}
}
Haschecked? Function (){
BeSureDel = confirm ("are you sure you want to delete the selected item? ");
} (): Function (){
Alert ("No selected item! ");
BeSureDel = false;
}();
Return beSureDel;
}
}, // Deleteall
GetByClass: function (classname, parent, nodename ){
Var s = (parent | document). getElementsByTagName (nodename | "*");
Return function (){
Var a = [];
For (var I = 0, j = s. length; I <j; I ++ ){
If (! S [I]. className) continue;
Var name = "" + s [I]. className + "";
If (name. indexOf ("" + classname + "")! =-1 ){
A. push (s [I]);
}
}
Return;
}();
},
$ C: function (classname, parent, nodename ){
Return YYJ. getByClass (classname, parent, nodename );
}, // GetByClass
$: Function (str ){
Return document. getElementById (str );
}, // GetById
Yyjtable: function (tableId ){
Var tbl = YYJ. $ (tableId );
Var trs = tbl. getElementsByTagName ("tr ");
For (var I = 1; I <trs. length; I ++ ){
If (I % 2! = 0 ){
Trs [I]. style. backgroundColor = "# FFFFF0 ";
} Else {
Trs [I]. style. backgroundColor = "white ";
}
Trs [I]. onmouseover = function (){
This. col1 = this. style. backgroundColor;
This. style. backgroundColor = "# FFFACD ";
}
Trs [I]. onmouseout = function (){
This. style. backgroundColor = this. col1;
}
}
}, // Yyjtable
/* Usage
YYJ. setCss ([YYJ. $ ("table1")], {
Color: "red ",
BackgroundColor: "silver"
});*/
SetCss: function (eles, opt ){
If (! Eles |! Opt) return;
If (! Eles. length ){
Throw new Error ("the first parameter of setCss must be an array! ");
}
For (var I = 0; j = eles [I]; I ++ ){
Try {
For (var x in opt ){
J. style [x] = opt [x];
}
} Catch (ex ){}
}
}, // SetCss
/* Usage
Var css = YYJ. getCss (YYJ. $ ("table1"), ["backgroundColor"]);
Alert (css ["backgroundColor"]); */
GetCss: function (ele, opt ){
If (! This. isArray (opt )){
Throw new Error ("the second parameter of getCss must be a string array! ");
}
Var css = {};
For (var I = 0, j = opt. length; I <j; I ++ ){
Try {
Css [opt [I] = ele. style [opt [I];
} Catch (ex ){}
}
Return css;

}, // GetCss
IsArray: function (opt ){
Return Object. prototype. toString. call (opt) = "[object Array]"
}

};
}
Function getUniqueInstance (){
If (UniqueInstance ){
Return UniqueInstance;
}
UniqueInstance = constrotor ();
Return UniqueInstance;
}
Return getUniqueInstance ();
}();
// Script execution time
/* Usage
Var ti = new YYJ. ticks ();
Ti. begin ();
Code segment
Ti. end ();
Alert (ti. tick );*/
YYJ. ticks = function (){
Var starttick, stoptick;
Return function (){
This. begin = function (){
Starttick = new Date ();
}
This. end = function (){
Stoptick = new Date ();
This. tick = stoptick-starttick;
}
}
}();

Related Article

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.