这个小框架已经应用到公司的项目,并开始逐步在前端展示页面上接替prototype.js。以后将围绕这个框架做一些应用。
复制代码 代码如下:
/*
* my javascript framework
* version : 1.0 beta
* author : misshjn
* email : misshjn@163.com
*/
function namespace(){};
function stringbuffer(){this.data = []};
var method = {
version : 1.0 beta,
element : function(){
this.version = method.version;
this.hide = function(){this.style.display=none; return this};
this.show = function(){this.style.display=; return this};
this.getstyle = function(s){
var value = this.style[s.camelize()];
if (!value){
if (this.currentstyle) {
value = this.currentstyle[s.camelize()];
}else if (document.defaultview && document.defaultview.getcomputedstyle) {
var css = document.defaultview.getcomputedstyle(this, null);
value = css ? css.getpropertyvalue(s) : null;
}
}
return value;
};
this.setstyle = function(s){
var slist = s.split(;);
for (var i=0,j; j=slist[i]; i++){
var k = j.split(:);
this.style[k[0].trim().camelize()] = k[1].trim();
}
return this;
};
this.toggle = function(){this.getstyle(display)==none?this.setstyle(display:):this.setstyle(display:none); return this};
this.hasclassname = function(c){return this.classname.hassubstring(c, )?true:false;};
this.addclassname = function(c){if(!this.hasclassname(c)){this.classname+= +c};return this};
this.removeclassname = function(c){if(this.hasclassname(c)){this.classname = ( +this.classname+ ).replace( +c+ , ).trim(); return this}};
this.getelementsbyclassname = function(c){return this.getelementsbyattribute(classname,c)};
this.getelementsbyattribute = function(n,v){ //name,value;
var elems = this.getelementsbytagname(*);
var elemlist = [];
for (var i=0,j; j=elems[i]; i++){
var att = j[n] || j.getattribute(n);
if (att==v){
elemlist.push(j);
}
}
return elemlist;
};
this.parentindex = function(p){
if (this==p){return 0}
for (var i=0,n=this; n=n.parentnode; i++){
if(n==p){return i;}
if(n==document.documentelement) return -1;
}
}
this.nextelement = function(){
var n = this;
for (var i=0,n; n = n.nextsibling; i++){
if(n.nodetype==1) return $(n);
}
return null;
};
this.previouselement = function(){
var n = this;
for (var i=0,n; n = n.previoussibling; i++){
if(n.nodetype==1) return $(n);
}
return null;
};
this.moveahead = function(){
if (this.previouselement()){
this.parentnode.insertbefore(this,this.previouselement());
}
return this;
};
this.moveback = function(){
var n = this.nextelement();
if (n){
this.parentnode.removechild(n);
this.parentnode.insertbefore(n,this);
}
return this;
};
},
array : function(){
this.indexof = function(){
for (i=0; i if (this[i]==arguments[0])
return i;
}
return -1;
};
this.lastindexof = function(){
for (i=this.length-1; i>=0; i--){
if (this[i]==arguments[0])
return i;
}
return -1;
};
this.each = function(fn){
for (var i=0,len=this.length; i fn(this[i]);
}
return this;
};
},
string : function(){
this.trim = function(){
var _re,_argument = arguments[0] || ;
typeof(_argument)==string?(_argument == ?_re = /(^\s*)|(\s*$)/g : _re = new regexp((^+_argument+*)|(+_argument+*$),g)) : _re = _argument;
return this.replace(_re,);
};
this.ltrim = function(){
var _re, _argument = arguments[0] || ;
typeof(_argument)==string?(_argument == ?_re = /(^\s*)/g : _re = new regexp((^+_argument+*),g)) : _re = _argument;
return this.replace(_re,);
};
this.rtrim = function(){
var _re, _argument = arguments[0] || ;
typeof(_argument)==string?(_argument == ?_re = /(\s*$)/g : _re = new regexp((+_argument+*$),g)) : _re = _argument;
return this.replace(_re,);
};
this.concat = function(){
var s = new stringbuffer();
s.append(this);
for (var i=0,j; j=arguments[i]; i++){
s.append(typeof j==object ? j.join() : j);
}
return s.tostring();
};
this.striptags = function(){
return this.replace(/]+>/gi, '');
};
this.cint = function(){
return this.replace(/\d/g,)-0;
};
this.camelize = function(){
return this.replace(/(-\s)/g,function($1){return $1.touppercase().substring(1,2)})
};
this.hassubstring = function(s,f){
if(!f) f=;
var v = (f+this+f).indexof(f+s+f);
return v==-1?false:v;
};
this.toxmlstring = function(){
var arr = arguments[0].split(&);
var str = new stringbuffer();
for (var i=0,len=arr.length; i var item = arr[i].split(=);
str.append(+item[0]+>);
}
return str.tostring();
};
this.urlencode = function(){ return encodeuricomponent(this)};
this.urldecode = function(){ return decodeuricomponent(this)};
},
stringbuffer : function(){
this.append = function(){this.data.push(arguments[0]);return this};
this.tostring = function(){return this.data.join(arguments[0]||)};
this.length = function(){return this.data.length};
},
namespace : function(){
this.copychild = this.appendchild = function(ns){
for (var key in ns){
this[key] = ns[key];
}
return this;
};
}
};
method.array.apply(array.prototype);
method.string.apply(string.prototype);
method.stringbuffer.apply(stringbuffer.prototype);
method.namespace.apply(namespace.prototype);
function $(){
var elem = typeof(arguments[0])==string?document.getelementbyid(arguments[0]):arguments[0];
if (!elem){return null}
if (elem[version]){return elem}
if ( arguments[1] == undefined || arguments[1]==true ){method.element.apply(elem);}
return elem;
};
$(document);
var ajax={
xmlhttp:function (){
try{
return new activexobject('msxml2.xmlhttp');
}catch(e){
try{
return new activexobject('microsoft.xmlhttp');
}catch(e){
return new xmlhttprequest();
}
}
}
};
ajax.request=function (){
if(arguments.length var para = {asynchronous:true,method:get,parameters:};
for (var key in arguments[1]){
para[key] = arguments[1][key];
}
var _x=ajax.xmlhttp();
var _url=arguments[0];
if(para[parameters].length>0) para[parameters]+='&_=';
if(para[method].touppercase()==get) _url+=(_url.match(/\?/)?'&':'?')+para[parameters];
_x.open(para[method].touppercase(),_url,para[asynchronous]);
_x.onreadystatechange=function (){
if(_x.readystate==4){
if(_x.status==200)
para[oncomplete]?para[oncomplete](_x):;
else{
para[onerror]?para[onerror](_x):;
}
}
};
if(para[method].touppercase()==post)_x.setrequestheader(content-type,application/x-www-form-urlencoded);
for (var reqheader in para[setrequestheader]){
_x.setrequestheader(reqheader,para[setrequestheader][reqheader]);
}
_x.send(para[method].touppercase()==post?(para[postbody]?para[postbody]:para[parameters]):null);
};
var cookies = {
get : function(n){
var dc = ; +document.cookie+; ;
var coo = dc.indexof(; +n+=);
if (coo!=-1){
var s = dc.substring(coo+n.length+3,dc.length);
return unescape(s.substring(0, s.indexof(; )));
}else{
return null;
}
},
set : function(name,value,expires){
var expdays = expires*24*60*60*1000;
var expdate = new date();
expdate.settime(expdate.gettime()+expdays);
var expstring = expires ? ; expires=+expdate.togmtstring() : ;
var pathstring = ;path=/;
document.cookie = name + = + escape(value) + expstring + pathstring;
},
del : function(n){
var exp = new date();
exp.settime(exp.gettime() - 1);
var cval=this.get(n);
if(cval!=null) document.cookie= n + =+cval+;expires=+exp.togmtstring();
}
}
function $a(list){
var arr = [];
for (var i=0,len=list.length; i arr[i] = list[i];
}
return arr;
}
function $d(str){return str.urldecode();}
function $e(str){return str.urlencode();}
function $v(id){return $(id).value}
function request(paras){
var url = location.href;
var parastring = &+url.substring(url.indexof(?)+1,url.length)+&;
if (parastring.indexof(&+paras+=)==-1){return };
parastring = parastring.substring(parastring.indexof(&+paras+=)+paras.length+2,parastring.length);
return parastring.substring(0,parastring.indexof(&));
}
