我有两个输入框来输入高度和宽度,还有一个按钮用JavaScript创建表格。 现在,我想使每个单元格的颜色在鼠标悬停时更改。最好使用jQuery。
function createTable(inRows, inCols) {
var inRows = document.getElementById('height').value;
var inCols = document.getElementById('length').value;
var body = document.getElementsByTagName("body")[0];
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
for (var i = 0; i < inRows; i++) {
var row = document.createElement("tr");
for (var j = 0; j < inCols; j++) {
var cell = document.createElement("td");
row.appendChild(cell);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
body.appendChild(tbl);
}
$(document).ready(function() {
$('td').hover(function() {
$(this).addClass('black');
});
});
在我这个初学者的脑海中,这看起来是正确的。但是...它并不是正确的。 我是否走对了方向?如何实现它?