<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!--编写HTML-->
<table border="1" cellspacing="0" cellpadding="2" rules="all" width="200">
<tr>
<td style="background-color: blue;"> </td>
<td style="background-color: brown;"> </td>
</tr>
<tr>
<td style="background-color: crimson;"> </td>
<td style="background-color: orange"> </td>
</tr>
</table>
<!--编写JavaScript-->
<script>
//找到页面中所有的td
var tds = document.getElementsByTagName('td');
for(var i=0; i<tds.length; i++){
//绑定单击事件
tds[i].onclick = function(){
//更换背景
document.body.style.backgroundColor = this.style.backgroundColor;
};
}
</script>
</body>
</html>
转载于:https://www.cnblogs.com/phpisfirst/p/9819142.html