主要是集成了常用的扩展,包括Dom与String、Array、StringBuffer、NameSpace等,当然还少不了Ajax。体积约8k。因为严格控制体积,所以功能有限。如果只要Ajax部分,就1k而已。
这个小框架已经应用到公司的项目,并开始逐步在前端展示页面上接替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<this.length; 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<len; 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<len; i++){
var item = arr[i].split("=");
str.append("<"+item[0]+"><![CDATA["+item[1]+"]]></"+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<2)return ;
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<len; 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("&"));
}
这个小框架已经应用到公司的项目,并开始逐步在前端展示页面上接替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<this.length; 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<len; 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<len; i++){
var item = arr[i].split("=");
str.append("<"+item[0]+"><![CDATA["+item[1]+"]]></"+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<2)return ;
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<len; 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("&"));
}
标签:
分享别人写的一个小型js框架
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“分享别人写的一个小型js框架”评论...
更新动态
2024年11月15日
2024年11月15日
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]
- 刘文正《流金三十年》[6N纯银镀膜][低速原抓WAV+CUE]
- 赵传.1994-精挑细选精选集【滚石】【WAV+CUE】
- 郑亚弦.2024-隔壁包厢603(EP)【发现梦想】【FLAC分轨】
- 文章.2004-被遗忘的时光【华博音乐】【WAV+CUE】
- 群星《青葱韶歌》原力计划·毕业季企划合辑[FLAC+分轨][661M]
- 群星《抖烧 DSD》抖音神曲 [WAV分轨][992M]