JavaScript简易猜数字
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript"> //得到1-10的随机数 var x = parseInt(Math.random()*10)+1; //alert(x); function myfn(){ var d = document.getElementById("d1"); var i = document.getElementById("i1"); var num = parseInt(i.value); if(num==x){ d.innerText="恭喜您 猜对了!"; }else if(num<x){ d.innerText="小了"; }else{ d.innerText="大了"; } } </script> </head> <body> <p>猜猜看</p> 请输入1-10随机的一个数:<input type="text" id="i1"> <input type="button" value="猜-猜" onclick="myfn()"> <div id="d1"></div> </body> </html>