test.prototype.testClick=function()
{
var button1 = document.getElementById("button1");
button1.onclick=function()
{
alert(this.testName);//这个this指向button1而非test对象本身
}
}
{
var button1 = document.getElementById("button1");
button1.onclick=function()
{
alert(this.testName);//这个this指向button1而非test对象本身
}
}
解决方案 »
- flash单击的时候调用html里的一个javascript自定义函数
- 用jquery必须把下载的js文件放在同一目录吗
- 获得frame里元素的值....
- 如何隐藏页面的css文件
- javascript页面跳转的went
- iframe的src属性怎么写?
- 如何将一个数组中的各项以表格的形式显示在页面上,谢谢大家
- 我要在jsp文件中,使用javascript,请问如何将jdbc中取得的数据放入javascript的数组中,能给示例代码吗?
- 关于字符串的问题
- 新手小白求解,哪里出问题啦
- 在FireFox里怎么能实现以下在IE能实现的功能?
- XML & js 创建的无限级非window式菜单的bug,请高人帮忙,谢谢
是的,请问有什么办法把test对象testName的值alert显示出来吗?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title> <script type="text/javascript">
var Class = {
create : function () {
return function () {
this.initialize.apply (this,arguments);
}
}
}
var $ = function (id) {
return document.getElementById(id);
}
var test = Class.create(); test.prototype = {
initialize : function (name) {
var t = this;
t.testName = name;
},
testName : null,
testClick : function (btnid) {
var t = this;
var button1 = $(btnid);
button1.onclick = function () {
alert(t.testName);
}
}
}
</script>
</head>
<body>
<button id="button1">
test</button>
<script type="text/javascript">
var t = new test('Andy');
t.testClick('button1');
</script>
</body>
</html>
相对于预编译的语言~这点很有特色~~~
<!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=utf-8" />
<title>Untitled 1</title>
<script type="text/javascript">
function test(newName)
{
this.testName = newName;
}
test.prototype.testClick=function()
{
var button1 = document.getElementById("button1");
var t=this
button1.onclick=function()
{
alert(t.testName);
}
}
</script>
</head>
<body>
<button id="button1">test</button>
<script type="text/javascript">
var t = new test('Andy');
t.testClick();
</script>
</body>
</html>