在一个工程里见到了这么一段代码求详解!
在此先谢谢!
var $ = function(id){return 'string' == typeof id ? document.getElementById(id) : id}
var $Name = function(name){return document.getElementsByName(name);}var $AddEvent = function(target, enentType, handle)
{
if(target.addEventListener) 
target.addEventListener(enentType, handle, false);
else if(target.attachEvent)
target.attachEvent('on' + enentType, handle);
else
target['on' + enentType] = handle;
}var $Bind = function(fun, thisObj)
{
return  function(){fun.apply(thisObj, arguments);};
}var Request = function(options){
var xmlHttpRequest;
try{
xmlHttpRequest = new XMLHttpRequest();
}catch(e){
xmlHttpRequest = new ActiveXObject('MSXML2.XMLHTTP');
}
this.xhr = xmlHttpRequest;
this.url = options.url || '';
this.data = options.data || '';
this.method = options.method || 'post';
this.onComplete = options.onComplete || function(){};
}Request.prototype = {
send : function(){
this.xhr.open(this.method.toUpperCase(), this.url, true);
this.xhr.onreadystatechange = $Bind(this.onStateChange, this);
this.xhr.send(this.data);
},
onStateChange : function(){
if(this.xhr.readyState == 4)
{
if(this.xhr.status == 200)
{
this.onComplete(this.xhr.responseText);
}
}
}
}

解决方案 »

  1.   


    var $ = function(id){return 'string' == typeof id ? document.getElementById(id) : id}
    //// 实例中 $("a")就能获取id为a 对象
    var $Name = function(name){return document.getElementsByName(name);}
    ////实例中 $Name("a")就能获取tag为a 对象的数组var $AddEvent = function(target, enentType, handle)
    {
    if(target.addEventListener)  
    target.addEventListener(enentType, handle, false);
    else if(target.attachEvent)
    target.attachEvent('on' + enentType, handle);
    else
    target['on' + enentType] = handle;
    }
    // 实例中 $AddEvent("a","click",func)---id为a的元素 的鼠标点击的时候 执行 func方法
      

  2.   


    var Request = function(options){
    var xmlHttpRequest;
    try{
    xmlHttpRequest = new XMLHttpRequest();
    }catch(e){
    xmlHttpRequest = new ActiveXObject('MSXML2.XMLHTTP');
    }
    this.xhr = xmlHttpRequest;
    this.url = options.url || '';
    this.data = options.data || '';
    this.method = options.method || 'post';
    this.onComplete = options.onComplete || function(){};
    }
    //封装ajax对象 a.Request({url:"url地址",data:"id=id&name=name",method:"post",onComplete=function(msg){alert(msg)}})