一段JS代码求详解

zhaoshe 2011-04-27 11:37:24
在一个工程里见到了这么一段代码求详解!
在此先谢谢!
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);
}
}
}
}
...全文
96 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰川711 2011-04-27
  • 打赏
  • 举报
回复
ajax
oggmm 2011-04-27
  • 打赏
  • 举报
回复
楼上正解!
hundanbaobao001 2011-04-27
  • 打赏
  • 举报
回复

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)}})


hundanbaobao001 2011-04-27
  • 打赏
  • 举报
回复

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方法

87,920

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧