注:假設(shè)以下所有函數(shù)都放在一個CC對象中,方便引用。
復(fù)制代碼 代碼如下:
//這個方法相信是最常用的了,
//它雖然沒有選擇器那么強大,但也有個小增強版,可查指定結(jié)點下ID所在的子元素
function $(id, p) {
//id是否是字符串,還是一個HTML結(jié)點
var iss = id instanceof String || typeof id == "string";
if (iss && !p)
return document.getElementById(id);
//如果是結(jié)點的話就直接返回該結(jié)點
if(!iss)
return id;
//如果id與p是同一個元素,直接返回
if(p.id == id)
return p;
//往父結(jié)點搜索
var child = p.firstChild;
while (child) {
if (child.id == id)
return child;
//遞歸搜索
var v = this.$(id, child);
if (v)
return v;
child = child.nextSibling;
}
//的確找不到就返回null
return null;
}
復(fù)制代碼 代碼如下:
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;
}
復(fù)制代碼 代碼如下:
//數(shù)組
function isArray(obj) {
return (typeof obj === "array" || obj instanceof Array);
},
//字符串
function isString(obj) {
return (typeof obj === "string" || obj instanceof String);
},
//函數(shù)
function isFunction(obj) {
return (typeof obj === "function" || obj instanceof Function);
},
//數(shù)字類型
function isNumber(ob) {
return (typeof ob === "number" || ob instanceof Number );
}
復(fù)制代碼 代碼如下:
// 返回表單可提交元素的提交字符串.
// 例如
// <form>
// <input type="text" name="user" value="rock" />
// <input type="text" name="password" value="123" />
// </form>
// 調(diào)用后就返回 user=rock&password=123
// 這些數(shù)據(jù)已經(jīng)過encodeURIComponent處理,對非英文字符友好.
// form元素中如果沒有name,則以id作為提供字符名.
function formQuery(f){
// f,一個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;
}
復(fù)制代碼 代碼如下:
/**
* 移除數(shù)組指定元素.
* 參數(shù)既可傳遞一個整形下標(biāo),也可傳遞一個數(shù)組數(shù)據(jù).
*/
Array.prototype.remove = (function(p) {
//參數(shù)為下標(biāo)
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;
}
//參數(shù)為數(shù)組數(shù)據(jù),最終要找到下標(biāo)來操作
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;
});
復(fù)制代碼 代碼如下:
Array.prototype.indexOf = (function(obj) {
for (var i = 0, length = this.length; i < length; i++) {
if (this[i] == obj) return i;
}
return - 1;
});
復(fù)制代碼 代碼如下:
/**
* 萬能而簡單的表單驗證函數(shù),這個函數(shù)利用了JS動態(tài)語言特性,看上去很神秘,
* 實際是很形象的,查看個例子就清楚了.
*/
validate: function() {
var args = CC.$A(arguments),
form = null;
//form如果不為空元素,應(yīng)置于第一個參數(shù)中.
if (!CC.isArray(args[0])) {
form = CC.$(args[0]);
args.remove(0);
}
//如果存在設(shè)置項,應(yīng)置于最后一個參數(shù)中.
//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) {
//如果在fomr中不存在該name元素,就當(dāng)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];
//選項
if (!d || typeof d != 'object') d = b;
//是否忽略空
if (!d.ignoreNull && (value == '' || value == null)) {
//如果不存在回調(diào)函數(shù),就調(diào)用alert來顯示錯誤信息
if (!d.callback) CC.alert(msg, obj, form);
//如果存在回調(diào),注意傳遞的三個參數(shù)
//msg:消息,obj:該結(jié)點,form:對應(yīng)的表單,如果存在的話
else d.callback(msg, obj, form);
//出錯后是否聚集
if (!d.nofocus) obj.focus();
result = false;
return false;
}
//自定義驗證方法
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);
//同上
else d.callback(msg, obj, form);
if (!d.nofocus) obj.focus();
result = false;
return false;
}
}
//如果不設(shè)置queryString并通過驗證,不存在form,就返回一個對象,
//該對象包含形如{elementName|elementId:value}的數(shù)據(jù).
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;
}
});
//如果設(shè)置的queryString:true并通過驗證,就返回form的提交字符串.
if (result !== false && form && queryStr) result = CC.formQuery(form);
return result;
}
復(fù)制代碼 代碼如下:
/**
* 應(yīng)用對象替換模板內(nèi)容
* templ({name:'Rock'},'<html><title>{name}</title></html>');
* st:0,1:未找到屬性是是否保留
*/
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;
}
}
return a;
});
}
JavaScript技術(shù):JavaScript 常用函數(shù)庫詳解,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。