【发布时间】:2017-06-17 22:48:17
【问题描述】:
您好,我需要一些关于 javascript 的帮助 我是自学新手
我如何编码 - 我有一个表格和 2 个按钮,当我按下按钮时,我想选择随机表格数据(从 1 到 9)并更改它的背景颜色
当我按下第二个按钮时 - 按顺序将表格数据从 1 更改为 9。
// 我的 javascript 知识非常低,但我知道我需要 为我的每个表数据提供 id, 带有 onclick="random()" 的按钮, 函数 random() - 但我不知道如何选择我为我的 td 提供的随机 ID
<script type="text/javascript">
//function random() {
// var myArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9");
// var x = Math.floor((Math.random() * 9) + 1);}
document.getElementById("random").onclick = function () {
document.getElementById("1").style.backgroundColor = "red";
}
</script>
<!DOCTYPE html>
<html>
<head>
<title>Javascript homework</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style type="text/css">
table {
border: 1px solid black;
}
th, td {
height: 150px;
width: 150px;
text-align: center;
color: black;
background-color: white;
font-size: 35px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<table>
<tr>
<td id="1">1</td>
<td id="2">2</td>
<td id="3">3</td>
</tr>
<tr>
<td id="4">4</td>
<td id="5">5</td>
<td id="6">6</td>
</tr>
<tr>
<td id="7">7</td>
<td id="8">8</td>
<td id="9">9</td>
</tr>
</table>
<button id="random" type="button" onclick="random()">Random</button>
<button id="next" type="button" onclick="next()">Next</button>
</div>
<div class="col-md-3"></div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
向我们展示您的代码以及您尝试过但不起作用的任何方法。
-
我知道的错误 javascript 代码,我知道如何在点击时更改元素,但不是评论中的随机元素,我正在尝试想出数组
标签: javascript html css button onclick