在一个工程里见到了这么一段代码求详解!
在此先谢谢!
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);
}
}
}
}
在此先谢谢!
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);
}
}
}
}
解决方案 »
- JS文件编码问题
- 带滚动条的窗口如何在拖拉滚动条后,弹出的窗口依然保持居中显示?
- 请高手指点,运行时总是提示“ The "xxx" object can not be found.”
- checkbox disabled="false" 后执行不了点击事件?
- 请问高手我想编这个赔率系统都需要掌握哪些知识?学习javascript能做出来吗?
- javascript能不能定义公用变量??
- 焦点在text框中,如何控制回车和提交做同样操作,也要求对页面的项目进行检查
- 请教frame的问题!
- 对下面还是不太清楚!哪位哥们给我讲讲
- 一般的网页可以接收参数吗?用javascript呢?
- struts2做上传与下载,上传成功后弹出下载框。
- jQuery的一些问题
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方法
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)}})