HTML+CSS+JavaScript

[HTML5+CSS3+Javascript 웹프로그래밍]9장 OpenChallenge

stonesy 2021. 11. 21. 02:20
728x90
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>마우스 클릭 연습</title>
	<style>
		table{
			border: 1px solid black;
			border-collapse: collapse;
		}
		td,th{
			border: 1px solid black;
		}
		img{
			width: 50px;
			height: 40px;
		}
	</style>
</head>
<body>
	<h3>마우스 클릭 연습</h3>
	<hr>
	<table id="gameboard"></table>

	<script>
		var select=0;
		var start_flag=1;

		//동적으로 게임판 생성
		var gameboard = document.getElementById("gameboard");
		for(var i=0;i<10;i++){
			var newtr = document.createElement("tr");
			for(var j=0;j<10;j++){
				var newtd = document.createElement("td");
				var newimg = document.createElement("img");
				newimg.setAttribute("src","./media/white.png");
				newimg.addEventListener("click",move);
				newtd.appendChild(newimg);
				newtr.appendChild(newtd);
			}
			gameboard.appendChild(newtr);
		}
		function move(){
			document.images[select].src="./media/white.png";
			select = Math.floor(Math.random()*100);
			document.images[select].src="./media/puppy.png";
		}
		move();
	</script>
</body>
</html>
728x90