因此,我正在使用JavaScript和HTML作爲學校和老師的項目製作井字遊戲遊戲,要求我們能夠「繪製」不同大小的板子, 3x3或4x4。「繪畫」不同大小的井字板
這就是我想要做的。
window.addEventListener("load", onLoad, false)
function onLoad() {
var wins = [7, 56, 448, 73, 146, 292, 273, 84];
var mainDiv = document.getElementById("mainDiv");
mainDiv.style.background = "#000000";
mainDiv.style.width = "306px";
mainDiv.style.height = "306px";
mainDiv.style.border = "solid";
mainDiv.style.borderColor = "#79BDEB";
mainDiv.style.position = "absolute";
mainDiv.style.left = "10px";
mainDiv.style.top = "70px";
//select.addEventListener("change", onChange, false);
}
function onChange() {
var select = document.getElementById("mySelect").value;
var main = document.getElementById("mainDiv");
if (select == 3) {
three();
} else if (select == 4) {
four();
} else if (select == 5) {
five();
} else {
six();
}
main.innerHTML = "";
counter = 0;
}
function three() {
var main = document.getElementById("mainDiv");
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var small = document.createElement("div");
main.appendChild(small);
small.style.background = "#52498F";
small.style.width = "100px";
small.style.height = "100px";
small.style.border = "solid";
small.style.borderColor = "#000000";
small.style.position = "absolute";
small.style.left = j * 100 + "px";
small.style.top = i * 100 + "px";
small.addEventListener("mousedown", onDown, false);
}
}
var state = false;
function onDown(e) {
var small = e.target;
small.style.background = "#f0f0f0";
small.style.fontSize = "72px";
small.style.fontFamily = "Arial";
if (!state) {
small.innerHTML = "x";
} else {
small.innerHTML = "o";
}
state = !state;
small.style.textAlign = "center";
}
}
function four() {
var main = document.getElementById("mainDiv");
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
var small = document.createElement("div");
main.appendChild(small);
small.style.background = "#52498F";
small.style.width = "75px";
small.style.height = "75px";
small.style.border = "solid";
small.style.borderColor = "#000000";
small.style.position = "absolute";
small.style.left = j * 75 + "px";
small.style.top = i * 75 + "px";
small.addEventListener("mousedown", onDown, false);
}
}
var state = false;
function onDown(e) {
var small = e.target;
small.style.background = "#f0f0f0";
small.style.fontSize = "60px";
small.style.fontFamily = "Arial";
if (!state) {
small.innerHTML = "x";
} else {
small.innerHTML = "o";
}
state = !state;
small.style.textAlign = "center";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tic-tac-toe</title>
<script src="logic.js"></script>
<style>
#mySelect {
font-size: 24px;
}
#myColor {
font-size: 24px;
}
</style>
</head>
<body>
<select id="mySelect" onchange="onChange()">
<option value="3">3 x 3</option>
<option value="4">4 x 4</option>
<option value="5">5 x 5</option>
<option value="6">6 x 6</option>
</select>
<select id="myColor" onchange="color()">
<option value="11">Blue</option>
<option value="12">Green</option>
<option value="13">Red</option>
</select>
<div id="mainDiv"></div>
<script type="text/javascript" src="cordova.js"></script>
</body>
</html>
多少onDown的,你呢? – artm 2014-09-28 12:57:45
四。我知道我只能使用一個。現在我正在改變它。 – 2014-09-28 14:47:10