* RegularJS framework
*
* verson: 0.1
* author: star.stroll
*
* 支持Firefox2与IE6或更高版本,其他浏览器待测.
*
******************************************************* */
/* ***************************
* 源生对象方法填补
*************************** */
// 字符串去除前后空格
String.prototype.trim = function (){
return this .replace( / (^\s*)|(\s*$) / g, "" );
};
// 字符串去除前空格
String.prototype.trimLeft = function (){
return this .replace( / ^\s* / g, "" );
};
// 字符串去除后空格
String.prototype.trimRight = function (){
return this .replace( / \s*$ / g, "" );
};
// 把对象转为Array对象,可转换NodeList,arguments等等
Array.toArray = function (obj){
var err;
try {
var ls = [];
for ( var i = 0 ;i < obj.length;i ++ ){
ls.push(obj[i]);
}
return ls;
} catch (err){
return null ;
}
};
// 移除数组中的成员
Array.prototype.removeItem = function (obj){
for ( var i = 0 ;i < this .length;i ++ ){
if ( this [i] == obj){
for ( var j = i;j < this .length - 1 ;j ++ ){
this [j] = this [j + 1 ];
}
this .pop();
}
}
return this ;
};
// 移除数组中的成员,相比removeItem速度要快,但循序不能维持原状
Array.prototype.removeItemFast = function (obj){
for ( var i = 0 ;i < this .length;i ++ ){
if ( this [i] == obj){
this [i] = this [ this .length - 1 ];
this .pop();
}
}
return this ;
};
// 移除数组中所有成员
Array.prototype.removeAll = function (){
while ( this .length != 0 ){
this .pop();
}
return this ;
};
// 效果与concat效果相似,区别在于pushAll将修改源数组
Array.prototype.pushAll = function (ls){
for ( var i = 0 ;i < ls.length;i ++ ){
this .push(ls[i]);
}
return this ;
};
// 将一个或多个成员插入到指定位置
Array.prototype.insert = function (){
if (arguments.length == 1 ){
this .push(arguments[ 0 ]);
}
var index = Number(arguments[ 0 ]);
if (arguments.length > 1 && index >= 0 && index < this .length){
var items = Array.toArray(arguments).slice( 1 );
var hl = this .slice( 0 ,index);
var fl = this .slice(index, this .length);
this .removeAll();
this .pushAll(hl.concat(items).concat(fl));
}
return this ;
};
// 将一个或多个数组中的所有成员插入到指定位置
Array.prototype.insertAll = function (){
if (arguments.length == 1 ){
this .pushAll(arguments[ 0 ]);
}
var index = Number(arguments[ 0 ]);
if (arguments.length > 1 && index >= 0 && index < this .length){
var items = Array.toArray(arguments).slice( 1 );
var concatItems = [];
for ( var i = 0 ;i < items.length;i ++ ){
concatItems.pushAll(items[i]);
}
var hl = this .slice( 0 ,index);
var fl = this .slice(index, this .length);
this .removeAll();
this .pushAll(hl.concat(concatItems).concat(fl));
}
return this ;
};
/* ***************************
* 以下是自定义对象与方法
*************************** */
// 简化扩展document.getElementById方法
$ = function (){
if (arguments.length > 1 ){
for ( var i = 0 , elements = [], length = arguments.length; i < length; i ++ ){
elements.push($(arguments[i]));
}
return elements;
}
return document.getElementById(arguments[ 0 ].toString());
};
// 简化扩展document.getElementsByName方法
$N = function (name){
if (arguments.length > 1 ){
for ( var i = 0 , elements = [], length = arguments.length; i < length; i ++ ){
elements.pushAll($N(arguments[i]));
}
return elements;
}
return Array.toArray(document.getElementsByName(arguments[ 0 ].toString()));
};
// 简化扩展document.getElementsByTagName方法
$TN = function (tagName){
if (arguments.length > 1 ){
for ( var i = 0 , elements = [], length = arguments.length; i < length; i ++ ){
elements.pushAll($TN(arguments[i]));
}
return elements;
}
return Array.toArray(document.getElementsByTagName(arguments[ 0 ].toString()));
};
// 判断对象是否有效
_ = function (obj){
return obj == null || obj == undefined;
};
// 判断是否为空字符串
_S = function (string){
return _(string) || string == "" ;
};
// 为尽可能使老JS应用兼容Firefox,尽可能少用
if (_(document.all)){
document.all = $TN( " * " );
}
// Event对象常用方法
EventUtil = {
// 获取浏览器源生Event对象
" getOriginalEventObject " : function (){
var err;
try {
var e = window.event;
if ( ! _(e)){
return e;
} else {
pcaller = this [ " getOriginalEventObject " ].caller;
while ( ! (e instanceof Event) && ! _(pcaller)){
e = pcaller.arguments[ 0 ];
pcaller = pcaller.caller;
}
return (e instanceof Event) ? e : undefined;
}
} catch (err){
return undefined;
}
},
// 获得更好兼容性的Event对象
" getEventObject " : function (){
var org = this .getOriginalEventObject();
if (_(org.target) && ! _(org.srcElement)){
org.target = org.srcElement;
}
}
};
2009-08-09 添加:
StringBuffer = function (string){
var buff = _(string) ? [] : [string];
var flag = true ;
var value = "" ;
this .append = function (string){
buff.push(string);
flag = true ;
return this ;
};
this .toString = function (){
if (flag){
value = buff.join( '' );
flag = false ;
}
return value;
};
this .length = function (){
return this .toString().length;
};
};
未完待续……