在此代码中,我想使用 JavaScript 使表格单元格可单击。
还请告诉我如何在点击事件中使用 i,j 值。
<!DOCTYPE html>
<html>
<head>
<style>
td
{
height : 30px;
width : 30px;
cursor: pointer;
}
</style>
<script>
function clickHere(){
var table = document.getElementById("myTable");
var row ;
var cell;
for(var i=0;i<2;i++){
row = table.insertRow(i);
for(var j=0;j<2;j++){
cell = row.insertCell(j);
}
}
}
</script>
</head>
<body onload="clickHere()">
<table id = "myTable" border="1"></table>
</body>
</html>
添加此代码:
cell.addEventListener("click",function(){
alert("cell clicked");
});
这段代码之后:
cell = row.insertCell(j);
它将向每个单元格添加事件侦听器。单击时会显示警报。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)