自己的JavaScript库_建设中....哈哈


(function(){
function _$(els){
this.elements = [];
for(var i = 0, len = els.length; i < len; i++){
var element = els[i];
if(typeof element === "string")
element = document.getElementById(element);
}
this.elements.push(element);
}
_$.prototype = {
each : function(fn){
for(var i = 0, len = this.elements.length; i < len; i++){
fn.call(this, this.elements[i]);
}
},
addEvent : function(type, fn){
var e = function(el){
if(window.addEventListener){
el.addEventListener(type, fn, false);
}else if(el.attachEvent){
el.attachEvent("on"+type, fn);
}
}
this.each(function(el){
e(el)
});
return this;
},
css : function(pre, val){
this.each(function(el){
el.style[pre] = val;
});
return this;
},
ajax : function(options){
options.method = options.method || "post";
options.param = options.param || null;
var createXhrObject = function(){
var methods = [
function(){return new XMLHttpRequest();},
function(){return new ActiveXObject('Msxml2.XMLHTTP');},
function(){return new ActiveXObject('Microsoft.XMLHTTP');}
];
for(var i = 0, len = methods.length; i < len ; i++){
try{
methods[i]();
}catch(e){
continue;
}
return methods[i]();
}
}
var xhr = createXhrObject();
xhr.onreadystatechange = function(){
if(xhr.readyState !== 4) return;
(xhr.status === 200) ?
options.callback.success(xhr.responseText, xhr.responseXML)
:
options.callback.failure(xhr.status);
}
xhr.open(options.method, options.url, true);
if(options.method !== 'post') options.param = null;
xhr.send(options.param.toString());
}
}
window.$ = function(){
return new _$(arguments);
}
})();

$(window).addEvent("load",function(){
$().ajax({
method : "post",//可省
url : "userAction_ajax.aspx",
param : 1,//可省
callback : {
success : function(responseText){
alert(responseText);
},
failure : function(statusCode){
alert(statusCode);
}
}
})
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值