HTML Memory Matching Game - Simple and Fun
<!DOCTYPE html>
<html>
<head>
<title>Memory Matching Game</title>
<style type="text/css">
body {
background-color: #ccc;
text-align: center;
}
#game-container {
max-width: 500px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
font-family: sans-serif;
}
#game-table {
margin: 0 auto;
border-collapse: collapse;
}
#game-table td {
width: 50px;
height: 50px;
border: 1px solid #000;
background-color: #ccc;
text-align: center;
font-size: 45px;
}
#game-table td.matched {
background-color: #3a3;
}
</style>
<script type="text/javascript">
window.onload = function() {
var allCells = document.getElementsByTagName('td');
var firstCell = null;
<pre><code> for(var i = 0; i < allCells.length; i++) {
allCells[i].onclick = function() {
this.innerHTML = this.getAttribute('data-value');
if(firstCell === null) {
firstCell = this;
} else {
if(firstCell.getAttribute('data-value') == this.getAttribute('data-value')) {
firstCell.className = 'matched';
this.className = 'matched';
} else {
setTimeout(function() {
firstCell.innerHTML = '';
this.innerHTML = '';
}, 500);
}
firstCell = null;
}
}
}
};
</script>
</code></pre>
</head>
<body>
<div id="game-container">
<h1>Memory Matching Game</h1>
<table id="game-table">
<tr>
<td data-value="1"> </td>
<td data-value="2"> </td>
<td data-value="3"> </td>
<td data-value="4"> </td>
</tr>
<tr>
<td data-value="1"> </td>
<td data-value="2"> </td>
<td data-value="3"> </td>
<td data-value="4"> </td>
</tr>
<tr>
<td data-value="5"> </td>
<td data-value="6"> </td>
<td data-value="7"> </td>
<td data-value="8"> </td>
</tr>
<tr>
<td data-value="5"> </td>
<td data-value="6"> </td>
<td data-value="7"> </td>
<td data-value="8"> </td>
</tr>
</table>
</div>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lnhU 著作权归作者所有。请勿转载和采集!