【发布时间】:2021-04-25 16:23:06
【问题描述】:
我正在尝试用对应于数组值的黄色为所有单元格着色。例如下图中所有的数字 1-12 都是可点击的并且有自己的功能。
当我单击 1 时,该函数会生成 1-25 的 4 个数字并将其存储在数组中。假设数字是 2,5,8,10。所以单元格 2、5、8、10 应该被涂成黄色。如何为数组值对应的单元格着色。
我的单元格具有代表其价值的 id。所以数字 1 的 id 是 '1' 等等。我想不出任何办法。
function func1() {
var random = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];
var selection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var numbers = [];
for (i = 0; i < 4; i++) {
var randomPositionl = Math.floor(Math.random() * random.length);
var final = random.splice(randomPositionl, 1);
console.log(final);
}
}
function func2() {
console.log(2);
}
function func3() {
console.log(3);
}
function func4() {
console.log(4);
}
function func5() {
console.log(5);
}
function func6() {
console.log(6);
}
function func7() {
console.log(7);
}
function func8() {
console.log(8);
}
function func9() {
console.log(9);
}
function func10() {
console.log(10);
}
function func11() {
console.log(11);
}
function func12() {
console.log(12);
}
table {
font-family: arial, sans-serif;
font-size: 16px;
border-collapse: collapse;
width: 100%;
margin-left: auto;
margin-right: auto;
table-layout: fixed;
text-align: center;
cursor: pointer;
color: white;
float: left;
text-shadow: 2px 2px 4px #000000;
background-color: black;
}
td {
border: 2px #a49e9b solid;
}
td:hover {
background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<table cellpadding="15" cellspacing="15">
<tr>
<td id="1" onclick="func1()">1</td>
<td id="3" onclick="func3()">3</td>
<td id="5" onclick="func5()">5</td>
<td id="7" onclick="func7()">7</td>
<td id="9" onclick="func9()">9</td>
<td id="11" onclick="func11()">11</td>
</tr>
<tr>
<td id="2" onclick="func2()">2</td>
<td id="4" onclick="func4()">4</td>
<td id="6" onclick="func6()">6</td>
<td id="8" onclick="func8()">8</td>
<td id="10" onclick="func10()">10</td>
<td id="12" onclick="func12()">12</td>
</tr>
</table>
</body>
</html>
【问题讨论】:
标签: javascript arrays