본문 바로가기
Python/Muzi

JS강화학습03. 로또번호 생성해서 출력하기

by 미눅스[멘토] 2023. 7. 12.
728x90

 

 

ex03.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<script>
function myclick(){
	var arr = []
	for(i=0; i<45; i++){
		arr.push(i+1)
	};
	
	for(i=0; i<100; i++){
	   var rnd = parseInt(Math.random()*45);
	   var r = arr[0];
       arr[0] = arr[rnd];
       arr[rnd] = r
	}
	
	console.log(arr)
// 	var td = document.getElementsByClassName('my_td');
	var objs = document.querySelectorAll(".my_td")
	console.log(objs)
	for(i=0; i<objs.length; i++){
		console.log(i)
		objs[i].innerHTML=arr[i];
	}
	
	
// 	objs[0].innerHTML = arr[0];
// 	objs[1].innerHTML = arr[1];
// 	objs[2].innerHTML = arr[2];
// 	objs[3].innerHTML = arr[3];
// 	objs[4].innerHTML = arr[4];
// 	objs[5].innerHTML = arr[5];
}




</script>

</head>
<body>
EX03
<table border="1px">
	<tr>
		<td class="my_td">__</td>
		<td class="my_td">__</td>
		<td class="my_td">__</td>
		<td class="my_td">__</td>
		<td class="my_td">__</td>
		<td class="my_td">__</td>
	</tr>
	<tr>
		<td colspan="6">
			<input type="button" value="생성하기" onclick="myclick()">
		</td>
	</tr>
 </table>
</body>
</html>

 

 

생성하기 클릭시

 

 

 

결과