2025-05-23

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
        <title>Insert title here</title>
        <script type="text/javascript">
                 /*
                * 先加載窗體
                * 生成一個1-100之間的隨機數
                * 獲取按鈕
                * 獲取text的value
                * 當點擊按鈕時,用text的value和隨機數比較
                *
                */
               window.onload = function(){
                var num = Math.floor(Math.random() * 100 + 1);
                var button = document.getElementById("button");
       var times = 0;
                button.onclick = function(){
                 var text = document.getElementById("inputText");
                 var value = text.value;
                 if(++times > 3){
         alert("您不適合玩此類遊戲");
         return;
        }
                 if(isNaN(value)){
                  alert("您輸入的不是一個數字,請輸入一個數字!");
                 }else if(value < 1 || value > 100){
                  alert("請輸入一個1-100的數字");
                 }else if(value == num){
                  alert("您猜對瞭!")
                 }else if(value > num){
                  alert("大瞭");
                 }else{
                  alert("小瞭");
                 }
        text.value = "";
                }
               }
        </script>
    </head>
    <body>
        請輸入一個1-100的整數: <input type="text" id="inputText"> <input type="button" id="button" value=" 猜猜猜 ">
    </body>
</html>

 

摘自  代俊建的專欄 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *