`

自己的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);
	        }
	    }
	})
})
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics