Simple HTML Guessing Game - Play Now!
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML Guessing Game - Play Now!</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #eee;
}
#game {
width: 400px;
margin: 20px auto;
text-align: center;
}
#btn {
background-color: #666;
padding: 10px;
color: #fff;
font-size: 18px;
border-radius: 3px;
cursor: pointer;
}
</style>
</head>
<body>
<div id='game'>
<h3>Can you guess the number?</h3>
<input type='text' id='guess' placeholder='Enter a number'>
<br><br>
<button id='btn'>Check Guess</button>
</div>
<script>
let randomNumber = Math.floor(Math.random() * 10);
console.log(randomNumber);
let btn = document.getElementById('btn');
btn.addEventListener('click', checkGuess);
function checkGuess(){
let guess = document.getElementById('guess').value;
if(guess == randomNumber){
alert('You guessed the number!');
} else {
alert('Sorry, try again!');
}
}
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lml3 著作权归作者所有。请勿转载和采集!