<html>
<head>
<script type="text/javascript">
function game(){//game类
}
game.prototype={
show:function(){
alert(1);
},
onkey:function(){
this.show();//在此处想调用show函数。当前情况下this不是指向game的(所以这种写法是错的),怎么才能调用到show函数
},
init:function(){
document.onkeydown=this.onkey;
}
}
var t=new game();
</script>
</head>
<body onload="t.init()">
</body>
</html>
解决方案 »
- [分享] jQuery开发之windows边栏小工具开发(Gadget development)- windows小工具实现的公司内部分机查询工具
- 2个窗口带框架,传递参数
- 请教怎么从div中获取内容,其中包含类似<h2>之类的标签?
- 使用script跳转的问题
- JS获取文件夹路径问题
- js乱码的问题!
- 求助:怎样固定两侧的浮动广告? (贴中含JS代码)
- 老问题,如何把F4键定义为表单提交的功能,既发送键?
- 如何在js中拦截alt+f4?
- 图片滚动显示
- post 提交表单,然后在另一个画面里取值时,提示对象没有定义。
- 不显示删除回复显示所有回复显示星级回复显示得分回复 #Eval("fnumber") 有问题!!!在线急等
<head>
<script type="text/javascript">
function game(){//game类
}
game.prototype={
show:function(){
alert(1);
},
onkey:function(){
t.show();//在此处想调用show函数。当前情况下this不是指向game的(所以这种写法是错的),怎么才能调用到show函数
},
init:function(){
document.onkeydown=this.onkey;
}
}
var t=new game();
</script>
</head>
<body onload="t.init()">
</body>
</html>
这样可以,你试试
所以还是希望在类的内部能解决。
另外 2楼,你不是女的38节就不关你事了?你老婆,或者你老妈,不是女的吗。
你的问题太标题当了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function game(){//game类
}
game.prototype={
show:function(){
alert(1);
},
onkey:function(){
this.show();//在此处想调用show函数。当前情况下this不是指向game的(所以这种写法是错的),怎么才能调用到show函数
},
init:function(){
var self = this;
document.onkeydown = function(){self.show()};
}
}
window.onload = function(){
new game().init();
}
</script>
<body >
</body>
</html>
<html>
<head>
<script type="text/javascript">
function game(){//game类
}
game.prototype={
show:function(){
alert(1);
},
onkey:function(){
this.show();
},
init:function(){
var _this = this;
document.onkeydown = function() {_this.onkey()};
}
}
var t=new game();
</script>
</head> <body onload="t.init()"> </body>
</html>
这个函数中能够调用到show(),我有特殊需求,onkey中代码比较长,这里只是
简化了下。
我的写法和6l一样
而且还节省了一个全局变量标题党+可耻.............