我有一个用javascript创建的表。当您点击它时,我想将单元格的颜色更改为红色。我知道我应该使用onClick事件。但我不确定如何在这项具体任务中使用它。
var width = parseInt(prompt("Put width", "here"));
var height = parseInt(prompt("Put height", "here"));
function myFunction() {
var table = document.getElementById("chessboard");
for (var i = 0; i < height; i++) {
var row = table.insertRow(i);
for (var j = 0; j < width; j++) {
row.insertCell(j);
}
};
}
&#13;
#chessboard {
border: 1px solid black;
border-collapse: collapse
}
td {
width: 40px;
height: 40px
}
tr:nth-child(odd) td:nth-child(even) {
background: black
}
tr:nth-child(even) td:nth-child(odd) {
background: black
}
&#13;
<body onload="myFunction()">
<div>
<table id="chessboard"></table>
</div>
</body>
&#13;
答案 0 :(得分:2)
您可以为每个向其添加selected
类的单元格添加onclick侦听器。您还需要跟踪之前选择的内容,以便从中删除selected
类:
var width = parseInt(prompt("Put width", "here"));
var height = parseInt(prompt("Put height", "here"));
// tracks the selected cell
var selectedCell = null;
// handles the clicks
function selectCell() {
// remove from previous if there is one
if (selectedCell != null) {
selectedCell.classList.remove('selected');
}
// mark cell as selected
selectedCell = this;
this.classList.add('selected');
}
function myFunction() {
var table = document.getElementById("chessboard");
for (var i = 0; i < height; i++) {
var row = table.insertRow(i);
for (var j = 0; j < width; j++) {
var cell = row.insertCell(j);
// bind the selectCell function to this cell
cell.onclick = selectCell.bind(cell);
}
};
}
&#13;
#chessboard {
border: 1px solid black;
border-collapse: collapse
}
td {
width: 40px;
height: 40px
}
tr:nth-child(odd) td:nth-child(even) {
background: black;
}
tr:nth-child(even) td:nth-child(odd) {
background: black;
}
td.selected {
background: red !important;
}
&#13;
<body onload="myFunction()">
<div>
<table id="chessboard"></table>
</div>
</body>
&#13;
答案 1 :(得分:1)
向所有单元格添加click
事件。
首先,您需要将原始颜色存储在某处,以便在再次点击时将其更改回正常状态。
为此,您可以使用data-*
属性。现在,要将正确的white
和black
颜色分配给相应的单元格&#39; data-color
属性,您可以使用此等效的JavaScript,
row.children[j].setAttribute('data-color', ((i % 2 != 0 && j % 2 == 0) || (i % 2 == 0 && j % 2 != 0)) ? 'black' : 'white')
这部分CSS,
tr:nth-child(odd) td:nth-child(even) {
background: black
}
tr:nth-child(even) td:nth-child(odd) {
background: black
}
现在,由于存储了原始颜色,您可以简单地提取存储在data-color
属性中的相应颜色,并在其backgroundColor
为red
时将其更改回正常。
var width = parseInt(prompt("Put width", "here"));
var height = parseInt(prompt("Put height", "here"));
function myFunction() {
var table = document.getElementById("chessboard");
for (var i = 0; i < height; i++) {
var row = table.insertRow(i);
for (var j = 0; j < width; j++) {
row.insertCell(j);
row.children[j].setAttribute('data-color', ((i % 2 != 0 && j % 2 == 0) || (i % 2 == 0 && j % 2 != 0)) ? 'black' : 'white')
row.children[j].addEventListener('click', function() {
this.style.backgroundColor = (this.style.backgroundColor == 'red') ? this.getAttribute('data-color') : this.style.backgroundColor = 'red';
});
}
};
}
&#13;
#chessboard {
border: 1px solid black;
border-collapse: collapse
}
td {
width: 40px;
height: 40px
}
tr:nth-child(odd) td:nth-child(even) {
background: black
}
tr:nth-child(even) td:nth-child(odd) {
background: black
}
&#13;
<body onload="myFunction()">
<div>
<table id="chessboard"></table>
</div>
</body>
&#13;