To this end, collected their usual use of some JavaScript functions, they are also common in other JS library, now collated and attached notes, convenient access, hope to help everyone.
Note: Suppose that all of the following functions are placed in a CC object for easy reference.
Copy Code code as follows:
This method is believed to be the most commonly used,
Although it is not as powerful as the selector, there is also a small enhanced version that can be found with the child element of the ID under the specified node.
function $ (ID, p) {
is the ID a string, or is it an HTML node?
var iss = ID instanceof String | | typeof id = = "string";
if (ISS &&!p)
return document.getElementById (ID);
If it is a knot, return directly to the node.
if (!iss)
return ID;
If the ID is the same element as P, return directly
if (p.id = = ID)
return p;
Search the parent node.
var child = P.firstchild;
while (child) {
if (child.id = = ID)
return to child;
Recursive search
var v = this.$ (ID, child);
if (v)
return v;
Child = child.nextsibling;
}
If you can't find it, return null.
return null;
}
Copy Code code as follows:
Each:function (object, callback, args) {
if (!object) {
return object;
}
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 < length; i++)
if (Callback.apply (Object[i], args) = = false) break;
} else {
if (object.length = = undefined) {
for (var name in object)
if (Callback.call (Object[name], name, object[name]) = = false) break;
else for (var i = 0, length = object.length, value = object[0];
I < length && Callback.call (value, I, value)!== false;
Value = Object[++i]) {}
}
return object;
}
Copy Code code 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);
},
Number Type
function Isnumber (ob) {
return (typeof ob = = "Number" | | ob instanceof number);
}
Copy Code code as follows:
//Returns the submission string for the form's submission element.
//For example
//<form>
//<input type= "text" name= "user" value= "Rock"/>
//<input type= "Tex T "name=" password "value=" 123 "/>
//</form>
//Call back after user=rock&password=123
///This data has been passed ENC Odeuricomponent handling, friendly to non-English characters. If there is no name in the
//form element, the character name is provided as an ID.
function Formquery (f) {
//F, a form form.
var formData = "", Elem = "", F = cc.$ (f);
var elements = f.elements;
var length = elements.length;
for (var s = 0; s < length; ++s) {
Elem = Elements[s];
if (elem.tagname = = ' INPUT ') {
if (Elem.type = = ' Radio ' | | | elem.type = = ' checkbox ') &&!elem.checked) {
Continue;
}
}
if (FormData!= "") {
FormData + + &;
}
FormData + + encodeuricomponent (elem.name| | Elem.id) + "="
+ encodeuricomponent (elem.value);
}
Return formData;
}
Copy Code code as follows:
/**
* Moves the divisor group to specify the element.
* Parameters can pass either a cosmetic subscript or an array of data.
*/
Array.prototype.remove = (function (p) {
parameter is 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;
}
parameter is an array of data, and eventually a subscript is found to operate
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 Code code 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 code as follows:
/**
* Universal and simple form validation functions, which take advantage of the JS Dynamic language features, look very mysterious,
* Actually is very image, look at an example is clear.
*/
Validate:function () {
var args = CC. $A (arguments),
form = null;
form if it is not an empty element, it should be placed in the first argument.
if (! Cc.isarray (Args[0])) {
form = cc.$ (Args[0]);
Args.remove (0);
}
If a setting item exists, it should be placed in the last argument.
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 the name element does not exist in the FOMR, the ID is obtained
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];
Options
if (!d | | typeof d!= ' object ') d = b;
Whether to ignore empty
if (!d.ignorenull && (value = = ' | | = value = NULL)) {
If no callback function exists, alert is invoked to display the error message
if (!d.callback) Cc.alert (msg, obj, form);
If there is a callback, note that the three parameters passed
MSG: Message, obj: The node, form: The corresponding form, if one exists
else D.callback (msg, obj, form);
Whether to gather after an error
if (!d.nofocus) Obj.focus ();
result = false;
return false;
}
Custom Validation Methods
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);
Ditto
else D.callback (msg, obj, form);
if (!d.nofocus) Obj.focus ();
result = false;
return false;
}
}
If you do not set the QueryString and verify that the form does not exist, an object is returned.
The object contains data such as {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 the querystring:true is set and validated, the submit string of the form is returned.
if (Result!== false && form && querystr) result = cc.formquery (form);
return result;
}
Copy Code code as follows:
/**
* Application Object Replace template content
* Templ ({name: ' Rock '}, ' * st:0,1: The property is not found to be reserved
/
Templ:function (obj, str, st) {
return str.replace (/\{([\w_$]+) \}/g, function (c, $) {
var a = obj[$1];
if (a = = = Undefined | | a = = NULL) {
if (st = = undefined) return ';
Switch (ST) {
Case 0:
return ';
Case 1:
return $;
Default:
return C;
}
}
return A;
});
}