To this end, I have collected some JavaScript Functions that I usually use. They are also common in other JS libraries. Now I want to sort them out and add comments for your convenience. I hope they will help you.
Note: assume that all of the following functions are stored in a CC object for reference.
CopyCode The Code is as follows: // This method is believed to be the most commonly used,
// Although it is not as powerful as the selector, it also has a small enhanced version, which can be used to query the child element of the ID under the specified Node
Function $ (ID, p ){
// Whether the ID is a string or an HTML Node
VaR ISS = ID instanceof string | typeof id = "string ";
If (ISS &&! P)
Return document. getelementbyid (ID );
// If it is a node, the node is directly returned.
If (! ISS)
Return ID;
// If ID and P are the same element, return directly
If (P. ID = ID)
Return P;
// Search for the parent node
VaR child = P. firstchild;
While (child ){
If (child. ID = ID)
Return child;
// Recursive search
VaR v = this. $ (ID, child );
If (V)
Return V;
Child = Child. nextsibling;
}
// If no result is found, null is returned.
Return NULL;
}
copy the Code the code is as follows: each: function (object, callback, argS) {
If (! Object) {
return object;
}< br> If (ARGs) {
If (object. length === undefined) {
for (VAR name in object)
If (callback. apply (object [name], argS) === false) break;
}else for (VAR I = 0, length = object. length; I If (callback. apply (object [I], argS) === false) break;
}else {
If (object. length = undefined) {
for (VAR name in object)
If (callbac K. call (object [name], name, object [name]) === false) break;
}else for (VAR I = 0, length = object. length, value = object [0];
I value = object [++ I]) {}< BR >}< br> return object;
}
copy the Code the code is as follows: // array
function isarray (OBJ) {
return (typeof OBJ = "array" | OBJ instanceof array);
},
// string
function isstring (OBJ) {
return (typeof OBJ = "string" | OBJ instanceof string);
},
// function
function isfunction (OBJ) {
return (typeof OBJ = "function" | OBJ instanceof function);
},
// numeric type
function isnumber (OB) {
return (typeof Ob = "Number" | ob instanceof number );
}
copy the Code the code is as follows: // return the submission string of the elements that can be submitted in the form.
// For example
//
//
//
//
// return after the call user = Rock & Password = 123
// The data has been processed by encodeuricomponent, it is friendly to non-English characters.
// if no name exists in the form element, ID is used as the provided character name.
function formquery (f ){
// F, a form.
var formdata = "", ELEM = "", F = cc. $ (f);
var elements = f. elements;
var length = elements. length;
for (var s = 0; S ELEM = elements [s];
If (ELEM. tagname = 'input') {
If (ELEM. type = 'Radio '| ELEM. type = 'checkbox ')&&! ELEM. Checked) {
continue;
}< BR >}< br> If (formdata! = "") {
formdata + = "&";
}< br> formdata + = encodeuricomponent (ELEM. name | ELEM. ID) + "="
+ encodeuricomponent (ELEM. value);
}< br> return formdata;
}
Copy codeThe Code is as follows :/**
* Removes the specified element from the array.
* The parameter can either pass an integer subscript or an array data.
*/
Array. Prototype. Remove = (function (p ){
// The parameter is a subscript.
If (CC. isnumber (p )){
If (p <0 | P> = This. Length ){
Throw "index of bounds:" + this. Length + "," + P;
}
This. splice (P, 1) [0];
Return this. length;
}
// The parameter is an array of data, and the subscript must be found for operation.
If (this. length> 0 & this [This. Length-1] = P ){
This. Pop ();
} Else {
VaR Pos = This. indexof (P );
If (Pos! =-1 ){
This. splice (Pos, 1) [0];
}
}
Return this. length;
});
Copy codeThe Code is as follows: array. Prototype. indexof = (function (OBJ ){
For (VAR I = 0, length = This. length; I <length; I ++ ){
If (this [I] = OBJ) return I;
}
Return-1;
});
Copy code The Code is as follows :/**
* The omnipotent and simple form verification function, which utilizes the dynamic JavaScript language features and looks mysterious,
* The actual image is very good. You can see the example clearly.
*/
Validate: function (){
VaR ARGs = cc. $ A (arguments ),
Form = NULL;
// If the form element is not empty, it should be placed in the first parameter.
If (! Cc. isarray (ARGs [0]) {
Form = cc. $ (ARGs [0]);
Args. Remove (0 );
}
// If any configuration item exists, it should be placed in the last parameter.
// Cfg. querystring = true | false;
// Cfg. Callback = Function
// Cfg. ignorenull
// Nofocus: True | false
VaR B = cc. isarray (B )? {}: Args. Pop (),
D;
VaR querystr = B. querystring,
Ignorenull = B. ignorenull,
CB = B. Callback;
VaR result = querystr? '':{};
Cc. Each (ARGs,
Function (I, V ){
// If this name element does not exist in fomr, it is obtained by ID.
VaR OBJ = V [0]. tagname? V [0]: form? Form [V [0]: CC. $ (V [0]);
// Console. debug ('checking field: ', V, 'current value:' + obj. value );
VaR value = obj. value,
MSG = V [1],
D = cc. isfunction (V [2])? V [3]: V [2];
// Option
If (! D | typeof D! = 'Object') d = B;
// Whether to ignore null
If (! D. ignorenull & (value = ''| value = NULL )){
// If the callback function does not exist, call alert to display the error message.
If (! D. Callback) CC. Alert (MSG, OBJ, form );
// If callback exists, pay attention to the three parameters passed
// MSG: Message, OBJ: node, form: corresponding form, if any
Else D. Callback (MSG, OBJ, form );
// Whether or not to aggregate after an error occurs
If (! D. nofocus) obj. Focus ();
Result = false;
Return false;
}
// Custom Verification Method
If (CC. isfunction (V [2]) {
VaR ret = V [2] (value, OBJ, form );
VaR pass = (Ret! = False );
If (CC. isstring (RET )){
MSG = ret;
Pass = false;
}
If (! Pass ){
If (! D. Callback) CC. Alert (MSG, OBJ, form );
// Same as above
Else D. Callback (MSG, OBJ, form );
If (! D. nofocus) obj. Focus ();
Result = false;
Return false;
}
}
// If querystring is not set and verified, an object is returned if no form exists,
// This object contains data in the shape of {elementname | elementid: Value.
If (querystr &&! Form ){
Result + = (result = '')?
(Typeof obj. Name = 'undefined' | obj. Name = '')? OBJ. ID: obj. Name) +
'=' + Value: '&' + V [0] + '=' + value;
} Else if (! Form ){
Result [V [0] = value;
}
});
// If querystring: True is set and verification is passed, the submission string of form is returned.
If (result! = False & Form & querystr) Result = cc. formquery (form );
Return result;
}
copy the Code the code is as follows: /**
* Application Object replacement template content
* Templ ({Name: 'Rock '}, ' {name} ');
* ST: the property not found is whether to retain
*/
Templ: function (OBJ, STR, St) {
return Str. replace (/\ {([\ W _ $] +) \}/g, function (C, $1) {
var A = OBJ [$1];
if (a = undefined | A = NULL) {
If (ST = undefined) return '';
switch (ST) {
case 0:
return '';
case 1:
return $1;
default:
return C;
}< BR >}< br> return a;
});
}