function testClass()
{
this.m_alert = "!!!";
this.body = document.body;
this.body.onclick = this.click;
}testClass.prototype.click = function()
{
alert(this.m_alert);
}window.onload = function()
{
try
{
var test = new testClass();
}
catch(e)
{
alert(e.messge);
}
}求教大家,我想通过将document.body与testClass类中body相关联,并且通过onclick来处罚click函数来输出testClass类中的m_alert成员变量,但是上述代码的运行结果为弹出undefined,这是则么回事,应该如何正确显示出m_alert的信息!!!,谢谢大家了(不好意思,我没有多少分数了)
解决方案 »
- 【求助】JQuery如何对$.post之后得到的html代码使用选择器?
- 怎么实现在网页中根据动态结果显示图片?比如网页中显示教学楼平面图中哪些教室在上课哪些教室空闲
- exths combo 选择问题
- firefox 用canvas画饼图,饼图下面被遮住一部分,怎么解决!
- 在一个INPUT中有默认的value值,当鼠标单机时清空value值,应该怎么写?
- 为什么对象为空啊?
- 100分!急!急!急!多窗口控制问题!
- **********parent.mainFrame.parent.history.go(-1)这句话有问题吗?@@@@@@@@@
- 如何在网页打开的时候把地址栏等去掉?
- js
- 判断页面是否刷新js
- javascript检测输入的值
<script>
function testClass()
{
this.m_alert = "!!!";
this.body = document.body;
this.body.onclick = this.click;
}testClass.prototype.click = function()
{
alert(this.m_alert);
}window.onload = function()
{
document.body.m_alert = "this is body m_alert";
try
{
var test = new testClass();
}
catch(e)
{
alert(e.messge);
}
}</script>
<script>
function testClass()
{
this.m_alert = "!!!";
this.body = document.body;
this.body.onclick = this.click(this.m_alert);
}testClass.prototype.click = function(v)
{
return function(){alert(v);}
};window.onload = function()
{
try
{
var test = new testClass();
}
catch(e)
{
alert(e.messge);
}
}</script>
可为什么会这样呢,红色标记的this难道不是指testClass这个类吗?
<script>
function testClass()
{
this.m_alert = "!!!";
this.body = document.body;
this.body.onclick = this.click;
} testClass.prototype.click = function()
{
alert(this.m_alert);
} window.onload = function()
{
document.body.m_alert = "this is body m_alert";
try
{
var test = new testClass();
test.click();//!!!
}
catch(e)
{
alert(e.messge);
}
} </script>
在body单击鼠标时,this肯定是指向body了