在此代码中,我想使用 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);
它将为每个单元格添加事件监听器。单击时将显示警报。
关于javascript - 如何在此代码中向表格单元格添加点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37589777/