JavaScript的问题:封装在类里面的Ajax,如何设置onreadystatechange事件的处理函数? 本帖最后由 aluzi 于 2010-03-14 15:12:53 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把var http = null;改成this.http = null;试一试还有onreadystatechange必须要用无参的函数 不行的。关键是“this”关键字很让人费解。 怎么不行??看看下面的代码function mysearch(){ this.http = null; this.id = "id1";}mysearch.prototype={ search:function() { var sUrl = "....."; if( this.http == null ) this.http = new ActiveXObject("Microsoft.XMLHTTP"); else if( this.http.readyState != 4 ) return false; alert(this.id); this.http.onreadystatechange = this.handleResult; this.http.open( "get", sUrl, true ); this.http.setRequestHeader( "CACHE-CONTROL", "nocache" ); this.http.send(); }, handleResult:function() {alert(this === window) //do something }}var a = new mysearch();a.search();要让类里的作用域为类实例即this指向本身,得用new 关键字实例化 var http;改为:this.http = null;this.http.onreadystatechange = this.handleResult;改为:var self = this;this.http.onreadystatechange = function(){self.handleResult()}; CKeditor 图片上传 完毕后的事件调用问题 上下可以滚动,但是怎么会左右滚动不了呢 怎么写这个的颜色的代码呀 javascript中如何引进PHP中的内容 光一个ocx控件,怎么得到clsid? 图片如何在同一个位置叠加(窗口大小改变后,位置没有相对变化)?急 将一个链接写入一个Function,应该怎么做呀 如何控制文本框输入的数值的大小范围(1-a变量之间,例如: 一个浏览网页硬盘就被共享的程序,有点问题向大虾请教!! onbeforeunload 能不能写自己的提示框 请教一下以下代码为什么找不到cookie文件 能用JS实现类似于CSDN的结帖给分的功能吗
改成this.http = null;试一试
还有onreadystatechange必须要用无参的函数
看看下面的代码function mysearch()
{
this.http = null;
this.id = "id1";
}
mysearch.prototype=
{
search:function()
{
var sUrl = ".....";
if( this.http == null )
this.http = new ActiveXObject("Microsoft.XMLHTTP");
else if( this.http.readyState != 4 )
return false;
alert(this.id);
this.http.onreadystatechange = this.handleResult;
this.http.open( "get", sUrl, true );
this.http.setRequestHeader( "CACHE-CONTROL", "nocache" );
this.http.send();
}, handleResult:function()
{alert(this === window)
//do something
}
}var a = new mysearch();
a.search();要让类里的作用域为类实例即this指向本身,得用new 关键字实例化
var http;
改为:
this.http = null;this.http.onreadystatechange = this.handleResult;
改为:
var self = this;
this.http.onreadystatechange = function(){self.handleResult()};