我刚刚开发了一些代码来创建一个 24x60 的表格。我想在以下位置打印每个的<td>
id mouseover
:
<!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 Document</title>
<style type="text/css">
table {
background-color:blue;
}
td {
width: 2px;
height: 2px;
background-color:red;
}
</style>
</head>
<body>
<table id="time-table"></table>
<script type="text/javascript">
var table = document.getElementById( "time-table" );
for ( var r = 0; r < 24; r++ ) {
var row = document.createElement( "tr" );
for ( var c = 0; c < 60; c++ ) {
var td = document.createElement( "td" );
td.id = "td-" + r + "-" + c;
td.onmouseover = function ( e ) {
console.log( this.id );
}
row.appendChild( td );
}
table.appendChild( row );
}
</script>
</body>
</html>
该代码有效,但现在我担心它是否经过优化?我是否在嵌套循环中创建了 1440 个事件处理函数?或者 JavaScript 解释器是否足够聪明,只能创建一个函数并将其分配给 1440 个<td>
元素?