萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

精確到分鐘的 JavaScript 日曆控件:日期時間選擇器完整代碼

前言

一般日曆控件僅支援選擇日期,本文提供精確到分鐘的日期時間選擇器,支援年、月、日、時、分完整選擇,介面美觀、代碼結構清晰,適合需精確時間輸入的表單場景。

核心特性

  • 年/月 下拉選單快速切換
  • 日期網格點擊選擇
  • 時/分 輸入框 + 上下按鈕微調 (24小時制、60分制)
  • 即時格式化輸出至指定表單欄位
  • 自動定位於輸入框附近、點擊外部關閉
  • 支援多實例 (每個輸入框獨立實例)

完整代碼結構 (節錄關鍵部分)

<style>
/* 關鍵樣式類別 */
.cal_table { border: 1px solid #333; border-collapse: collapse; background: #fff; font-size: 12px; }
.cal_drawdate { width: 100%; border-collapse: collapse; }
.cal_drawdate td { border: 1px solid #fff; text-align: center; cursor: pointer; }
.cal_drawtime { font-size: 12px; }
.m_frameborder { width: 35px; height: 19px; border: 1px inset #D4D0C8; background: #fff; text-align: right; }
.m_arrow { width: 16px; height: 8px; background: #ccc; font-family: Webdings; font-size: 7px; cursor: default; }
.m_input { width: 12px; height: 14px; border: 0; font-family: Tahoma; font-size: 9px; text-align: right; }
</style>

<script>
function CalendarMinute(name, fName) {
    this.name = name;           // 全域變數名,供內部事件呼叫
    this.fName = fName || 'm_input'; // 輸入框 class
    this.timer = null;
    this.fObj = null;           // 當前聚焦的輸入框

    // 產生完整 HTML (年月下拉、日期表格、時分輸入)
    this.toString = function () {
        var d = new Date();
        var h = this.formatTime(d.getHours());
        var m = this.formatTime(d.getMinutes());
        var s = '';
        s += '<table class="cal_drawtime" cellspacing="0" cellpadding="0"><tr><td>請選擇時間:</td><td>';
        s += '<div class="m_frameborder">';
        s += '<input radix="24" value="' + h + '" class="' + this.fName + '" onfocus="' + this.name + '.setFocusObj(this)" onblur="' + this.name + '.setTime(this)" onkeyup="' + this.name + '.prevent(this)" onkeypress="if (!/[0-9]/.test(String.fromCharCode(event.keyCode)))event.keyCode=0" onpaste="return false" ondragenter="return false">:';
        s += '<input radix="60" value="' + m + '" class="' + this.fName + '" onfocus="' + this.name + '.setFocusObj(this)" onblur="' + this.name + '.setTime(this)" onkeyup="' + this.name + '.prevent(this)" onkeypress="if (!/[0-9]/.test(String.fromCharCode(event.keyCode)))event.keyCode=0" onpaste="return false" ondragenter="return false">';
        s += '</div></td><td>';
        s += '<table class="cal_drawtime" border="0" cellspacing="0" cellpadding="0">';
        s += '<tr><td><button id="' + this.fName + '_up" class="m_arrow" onfocus="this.blur()" onmouseup="' + this.name + '.controlTime()">5</button></td></tr>';
        s += '<tr><td><button id="' + this.fName + '_down" class="m_arrow" onfocus="this.blur()" onmouseup="' + this.name + '.controlTime()">6</button></td></tr>';
        s += '</table></td></tr></table>';
        // 此處省略日期表格生成代碼 (與傳統日曆類似)
        return s;
    };

    this.formatTime = function (n) { return n < 10 ? '0' + n : n; };
    this.play = function () { this.timer = setInterval(this.doPlay.bind(this), 100); };
    this.doPlay = function () { /* 持續更新時分顯示 */ };
    this.controlTime = function () { /* 上下按鈕遞增/遞減邏輯 */ };
    this.setFocusObj = function (obj) { this.fObj = obj; };
    this.setTime = function (obj) { /* 失焦時驗證並寫回主輸入框 */ };
    this.prevent = function (obj) { /* 限制只能輸入數字 */ };
    // ... 其餘方法:日期切換、今日高亮、點擊日期填入等
}

// 使用方式
var cal = new CalendarMinute('cal1', 'myInput');
document.write(cal.toString());
cal.play(); // 啟動時鐘更新
</script>

使用說明

  1. 在頁面引入上述 CSS 與 JS。
  2. 實例化:var cal = new CalendarMinute('cal1', 'inputClass');
  3. 在需要顯示日曆的位置寫入:document.write(cal.toString()); 或將 cal.toString() 內容放入容器 innerHTML。
  4. 呼叫 cal.play() 啟動時分即時更新。
  5. 目標輸入框加上 class="inputClass" 並綁定 onclick 顯示/定位日曆層。

總結

此日曆控件提供完整的日期時間選擇功能,代碼雖屬早期風格 (大量 document.write、內聯事件),但邏輯完整,可作為學習日期時間選擇器實現原理的參考,或經現代化重構 (ES6 Class、事件委託、CSS 變數、模組化) 後投入生產使用。

copyright © 萬盛學電腦網 all rights reserved