javascript封装整理
// JavaScript Document
/*update by 2011.3.29*/
/*getElementById
* @param {String} id ID值
*/
var $$ = function(id){
if(typeof id!="undefined" && typeof id === "string"){
return document.getElementById(id);
}
return null;
}
/*添加事件
* @param {Object} oTarget 对象
* @param {String} sEventType 事件类型
* @param {Function} fnHandler 事件方法
*/
var addEventHandler=function(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
}
/*注销事件
* @param {Object} oTarget 对象
* @param {String} sEventType 事件类型
* @param {Function} fnHandler 事件方法
*/
var romoveEventHandler=function(oTarget, sEventType, fnHandler) {
if (oTarget.removeEventListener) {
oTarget.removeEventListener(sEventType, fnHandler, false);
} else if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = "";
}
}
/*json扩展
* @param {Object} target 目标json
* @param {Object} src 源json
*/
var extendJson=function(target,src){
for(var para in src){
target[para]=src[para];
}
return target;
}
/*在目标元素之后插入新元素 js自带方法: target.appendChild(newDoc);target.insertBefore(newDoc,existingChild);
* @param {Document} newEl 新元素
* @param {Document} targetEl 目标元素
*/
var insertAfter=function(newEl,targetEl){
var parentEl = targetEl.parentNode;
if(parentEl.lastChild == targetEl){
parentEl.appendChild(newEl);
}else{
parentEl.insertBefore(newEl,targetEl.nextSibling);
}
}
/*动态加载CSS文件
* @param {String} file css路径
* @param {String} cssid css link ID
*/
var loadCSS=function (file,cssid){
var cssTag = cssid ? document.getElementById(cssid) : null;
var head = document.getElementsByTagName('head').item(0);
if(cssTag) head.removeChild(cssTag);
css = document.createElement('link');
css.href = file;
css.rel = 'stylesheet';
css.type = 'text/css';
if(cssid){css.id = cssid;}
head.appendChild(css);
}
/*ajax封装
* @param {Object} options 参数集
* @param {String} url 链接
* @param {String} type 传参方式 "POST" or "GET"(默认)
* @param {Bool} async 是否异步 true异步(默认) false同步
* @param {String} dataType 返回数据类型 "html"(默认) "xml" "json"
* @param {Function} beforeSend 发送请求前调用函数
* @param {Function} success 请求成功后回调函数
* @param {Function} complete 请求完成后回调函数(不管成功与否)
*/
var ajaxFun = function(options){
var ajaxops={
url:"",
type:"GET",
async:true,
dataType:"html",
beforeSend:null,
success:function(){},
complete:null
}
var ajaxops = extendJson(ajaxops,options);
if(ajaxops.url){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
var requestDone=false;
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState===4){
if(( xmlHttp.status >= 200 && xmlHttp.status < 300 ) || xmlHttp.status === 304 || xmlHttp.status === 1223 || xmlHttp.status === 0){
var msg;
switch(ajaxops.dataType){
case "html":
msg=xmlHttp.responseText;
break;
case "xml":
msg=xmlHttp.responseXML;
break;
case "json":
msg=xmlHttp.responseText;
msg=eval("("+msg+")");
break;
default:
msg=xmlHttp.responseText;
break;
}
ajaxops.success(msg);
}
if(ajaxops.complete && !requestDone){
ajaxops.complete(msg);
requestDone=true;
}
}
}
if(ajaxops.beforeSend){
ajaxops.beforeSend();
}
xmlHttp.open(ajaxops.type,ajaxops.url,ajaxops.async);
xmlHttp.send(null);
}
}
/*
* $class 写类工具函数
* @param {Function} constructor
* @param {Object} prototype
* write by Snandy http://www.cnblogs.com/snandy/
*/
var $class = function(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {};
return function() {
for(var atr in p) {
arguments.callee.prototype[atr] = p[atr];
}
c.apply(this,arguments);
}
}
/*update by 2011.3.29*/
/*getElementById
* @param {String} id ID值
*/
var $$ = function(id){
if(typeof id!="undefined" && typeof id === "string"){
return document.getElementById(id);
}
return null;
}
/*添加事件
* @param {Object} oTarget 对象
* @param {String} sEventType 事件类型
* @param {Function} fnHandler 事件方法
*/
var addEventHandler=function(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
}
/*注销事件
* @param {Object} oTarget 对象
* @param {String} sEventType 事件类型
* @param {Function} fnHandler 事件方法
*/
var romoveEventHandler=function(oTarget, sEventType, fnHandler) {
if (oTarget.removeEventListener) {
oTarget.removeEventListener(sEventType, fnHandler, false);
} else if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = "";
}
}
/*json扩展
* @param {Object} target 目标json
* @param {Object} src 源json
*/
var extendJson=function(target,src){
for(var para in src){
target[para]=src[para];
}
return target;
}
/*在目标元素之后插入新元素 js自带方法: target.appendChild(newDoc);target.insertBefore(newDoc,existingChild);
* @param {Document} newEl 新元素
* @param {Document} targetEl 目标元素
*/
var insertAfter=function(newEl,targetEl){
var parentEl = targetEl.parentNode;
if(parentEl.lastChild == targetEl){
parentEl.appendChild(newEl);
}else{
parentEl.insertBefore(newEl,targetEl.nextSibling);
}
}
/*动态加载CSS文件
* @param {String} file css路径
* @param {String} cssid css link ID
*/
var loadCSS=function (file,cssid){
var cssTag = cssid ? document.getElementById(cssid) : null;
var head = document.getElementsByTagName('head').item(0);
if(cssTag) head.removeChild(cssTag);
css = document.createElement('link');
css.href = file;
css.rel = 'stylesheet';
css.type = 'text/css';
if(cssid){css.id = cssid;}
head.appendChild(css);
}
/*ajax封装
* @param {Object} options 参数集
* @param {String} url 链接
* @param {String} type 传参方式 "POST" or "GET"(默认)
* @param {Bool} async 是否异步 true异步(默认) false同步
* @param {String} dataType 返回数据类型 "html"(默认) "xml" "json"
* @param {Function} beforeSend 发送请求前调用函数
* @param {Function} success 请求成功后回调函数
* @param {Function} complete 请求完成后回调函数(不管成功与否)
*/
var ajaxFun = function(options){
var ajaxops={
url:"",
type:"GET",
async:true,
dataType:"html",
beforeSend:null,
success:function(){},
complete:null
}
var ajaxops = extendJson(ajaxops,options);
if(ajaxops.url){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
var requestDone=false;
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState===4){
if(( xmlHttp.status >= 200 && xmlHttp.status < 300 ) || xmlHttp.status === 304 || xmlHttp.status === 1223 || xmlHttp.status === 0){
var msg;
switch(ajaxops.dataType){
case "html":
msg=xmlHttp.responseText;
break;
case "xml":
msg=xmlHttp.responseXML;
break;
case "json":
msg=xmlHttp.responseText;
msg=eval("("+msg+")");
break;
default:
msg=xmlHttp.responseText;
break;
}
ajaxops.success(msg);
}
if(ajaxops.complete && !requestDone){
ajaxops.complete(msg);
requestDone=true;
}
}
}
if(ajaxops.beforeSend){
ajaxops.beforeSend();
}
xmlHttp.open(ajaxops.type,ajaxops.url,ajaxops.async);
xmlHttp.send(null);
}
}
/*
* $class 写类工具函数
* @param {Function} constructor
* @param {Object} prototype
* write by Snandy http://www.cnblogs.com/snandy/
*/
var $class = function(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {};
return function() {
for(var atr in p) {
arguments.callee.prototype[atr] = p[atr];
}
c.apply(this,arguments);
}
}