需求
实现一个猜数字的功能
效果
JS
思路:生成一个随机数字,然后获取用户每次输入的数字进行判断
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#text{
width: 95px;
height: 95px;
text-align: center;
font-size: 25px;
}
#btn{
width: 100px;
height: 30px;
}
#label{
width:96px;
height: 50px;
text-align: center;
}
</style>
</head>
<body>
<input type="text" id="text"/><br><br>
<input type="button" id="btn" value="确定" /><br><br>
<input id="label" disabled="disabled"></input>
<script type="text/javascript">
var b = document.getElementById("btn");
var t = document.getElementById("text");
var l = document.getElementById("label");
var n = parseInt(Math.random()*100 + 1);
b.onclick = function(){
if(t.value == n){
l.value = "恭喜你中奖啦";
} else if(t.value < n && t.value != 0){
l.value = "小了";
} else if(t.value > n){
l.value = "大了";
} else if(t.value == 0){
l.value = "你还没有输入";
}
}
</script>
</body>
</html>