[html]此代码动态创建表格。
问:如何给创建出的td添加onclick事件。
<html>
<head>
<title></title>
</head><style>
.td_css{height:50px;width:60px; background-color:#ccc;}
</style>
<body style="text-align:center;" onload="creatTab(5,5)">
<table border="1" id="tab">
</table>
</body><script type="text/javascript">
function creatTab(rows,cols)
{
for (var i = 0; i < parseInt(rows); i++)
{
var _tr = tab.insertRow(); //创建行
for (var j = 0; j < parseInt(cols); j++)
{
var _td = _tr.insertCell(); //创建单元格
_td.id = "td_" + i.toString() + "_" + j.toString();
_td.className = "td_css";
// _td.setAttribute('onclick', 'onss(this)'); // 此方式不好用
// _td.onclick='onss(this)'; //此方式也不好用
}
}
// alert(tab.innerHTML);
}
function onss(tdID) //
{
alert(tdID.id);
} </script>
</html>
问:如何给创建出的td添加onclick事件。
<html>
<head>
<title></title>
</head><style>
.td_css{height:50px;width:60px; background-color:#ccc;}
</style>
<body style="text-align:center;" onload="creatTab(5,5)">
<table border="1" id="tab">
</table>
</body><script type="text/javascript">
function creatTab(rows,cols)
{
for (var i = 0; i < parseInt(rows); i++)
{
var _tr = tab.insertRow(); //创建行
for (var j = 0; j < parseInt(cols); j++)
{
var _td = _tr.insertCell(); //创建单元格
_td.id = "td_" + i.toString() + "_" + j.toString();
_td.className = "td_css";
// _td.setAttribute('onclick', 'onss(this)'); // 此方式不好用
// _td.onclick='onss(this)'; //此方式也不好用
}
}
// alert(tab.innerHTML);
}
function onss(tdID) //
{
alert(tdID.id);
} </script>
</html>
_td.onclick=function(){
onss(this)
};