自定义一个类,类内部使用DOM方法生成一些网页元素,例如button,如何将onclick定义为调用这个类的某个方法?

CIT 2008-01-25 01:25:09
window.onload=function(){
var GridTable = function(){
this.id = arguments[0];
this.button = document.getElementById('button');//此段方法应为生成一个button,为方便提问而进行了简化

this.show = gridshow;
this.tt=function(){alert('this.tt()');};
}

function gridshow(){
document.getElementById('div').innerHTML='<input type="button" name="button" id="button" value="按钮" onclick="'+this.id+'.tt();" />'
//var x=this.id;
//this.button.onclick=function(x)
// {alert(x)};
}

grid1 = new GridTable('grid1');
grid1.show();

}
注意function gridshow(),现在是直接修改div内的html代码将onclick事件指定为“grid1.tt();“;
而我希望能使用DOM的标准方法来设定onclick
可是IE不支持DOM的button.setAttribute('onclick',this.id+'.tt();'),只能用button.onclick = function(){......};的方法,而现在我无法将grid1的句柄传给onclick的事件中……
望高人指点~~~
...全文
61 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cloudgamer 2008-01-25
  • 打赏
  • 举报
回复
<!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>

<body>


<div id="a"></div>


<script>
window.onload=function(){

var GridTable = function(){
var o = this;

this.tt = function(){alert('1');};

this.show = gridshow;
}

function gridshow(){
var o = this;

var b = document.createElement('input');
b.type = "button";
b.name = "b";
b.id = "b";
b.value = "按钮";

b.onclick = function(){ o.tt() };

document.getElementById('a').appendChild(b);
}

grid1 = new GridTable();
grid1.show();

}

</script>



</body>
</html>
cloudgamer 2008-01-25
  • 打赏
  • 举报
回复
<!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>

<body>


<div id="a"></div>


<script>
window.onload=function(){

var GridTable = function(){
var o = this;

this.tt = function(){alert('1');};

this.show = gridshow;
}

function gridshow(){
var o = this;

var b = document.createElement('input');
b.type = "button";
b.name = "b";
b.id = "b";
b.value = "按钮";

b.onclick = function(){ o.tt() };

document.getElementById('a').appendChild(b);
}

grid1 = new GridTable();
grid1.show();

}

</script>



</body>
</html>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧