本帖最後由 李允軒 於 2014-7-21 13:55 編輯
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset "UTF-8">
- <title>我的占卜小程式</title>
- </head>
- <body>
- 年:<input id="iyear" /><br />
- 月:<input id="imonth" /><br />
- 日:<input id="iday" /><br />
- <button onclick="calc();">按鈕</button>
- <div id = "result">請輸入出生年月日。</div>
- <script type="text/javascript">
- function inputRange(value,min,max) {
- return Math.min(max,Math.max(min,value));
- }
-
- function calc () {
- var y = inputRange(parseInt(document.getElementById("iyear").value), 1900, 2014);
- var m = inputRange(parseInt(document.getElementById("imonth").value),1, 12);
- var isLeapYear = false;
- if(y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)
- {
- isLeapYear = true;
- }
- var maxdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- maxdays[1] = (isLeapYear)? 29 : 28;
- var d = inputRange(parseInt(document.getElementById("iday").value),1, maxdays[m - 1]);
- var r = ((y + m) * d) % 3;
- var a = ["吉", "一般", "凶"];
- document.getElementById("result").innerHTML = "您輸入的日期是:" + y + "/" + m + "/" + d + "<br />" + "您的運勢是:" + a[r] ;
- document.getElementById("iyear").value = y;
- document.getElementById("imonth").value = m;
- document.getElementById("iday").value = d;
- }
- </script>
- </body>
- </html>
複製代碼 |