DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<button class = 'reset'>开始新的一局button>
<div>请输入你猜的数字:
<input type="text" class="number">
<button class="guess">猜!button>
div>
<div> 猜的次数: <span class="count">0span> div>
<div>结果:<span class="result">span>div>
<script>
let number = document.querySelector('.number');
let count = document.querySelector('.count');
let result = document.querySelector('.result');
let reset = document.querySelector('.reset');
let guess = document.querySelector('.guess');
let numberToGuess = parseInt(Math.random() * 100 + 1);
console.log(numberToGuess);
let tmp = 0;
guess.onclick = function(){
tmp++;
count.innerText = tmp;
console.log(tmp);
let userGuessNumber = parseInt(number.value);
if(userGuessNumber == numberToGuess){
result.innerHTML = 'bingo!';
result.style = 'color: orange';
} else if (userGuessNumber < numberToGuess){
result.innerHTML = '猜小了!';
result.style = 'color: green';
} else {
result.innerHTML = '猜大了!';
result.style = 'color: red';
}
};
reset.onclick = function(){
numberToGuess = parseInt(Math.random() * 100 + 1);
tmp = 0;
count.innerText = tmp;
result.innerText = "";
number.value = "";
}
script>
body>
html>