又一日曆輸入效果沒考慮相容性,IE通過_時間日期

來源:互聯網
上載者:User

<script> onload = function() { var panel = new SelectPanel; var rili = new Kalendar; panel.Create(); rili.Create(panel); panel.Apply(document.form1.a); panel.Apply(document.form1.b); } </script> <form name="form1"> <input name="a" readonly> <input name="b" readonly> </form> <script> /* 日曆選擇類 this.Width = 0; // 設定日曆寬度,為零時不指定寬度。 this.Height = 0; // 設定日曆高度,為零時不指定高度。 this.BorderColor = "#1095c6"; // 設定日曆邊框顏色。 this.BackColor = "white"; // 設定日曆背景顏色。 this.CapBack = "#d9ebfe"; // 設定日曆深色。 this.Tag = doc.createElement("table"); // 日曆DOM節點。 this.Create = function(container) // 將日曆綁定到指定對象。 */ function Kalendar(win) { if(!win) win = self; var doc = win.document; var _Kalendar = this; this.Width = 0; this.Height = 0; this.BorderColor = "#1095c6"; this.BackColor = "white"; this.CapBack = "#d9ebfe"; this.Tag = doc.createElement("table"); this.Tag.style.cursor = "default"; this.Create = function(container) { if(!container) container = doc.body; this.Parent = container; this.Tag.style.borderCollapse = "collapse"; this.Tag.border = 1; this.Tag.borderColor = this.BorderColor; this.Tag.width = this.Width ? this.Width : ""; this.Tag.height = this.Height ? this.Height : ""; this.Tag.rows[0].cells[0].bgColor = this.CapBack; (container.Tag || container).appendChild(this.Tag); BuildDateFace(new Date(sltNian.value, sltYue.value)); } var today = new Date; var tr = this.Tag.insertRow(); tr.insertCell(); var sltNian = doc.createElement("select"); sltNian.options.add(new Option(today.getFullYear() + "年", today.getFullYear())); InitYearSelect(); function InitYearSelect() { while(sltNian.selectedIndex < 10) sltNian.options.add(new Option(sltNian.options[0].value - 1 + "年", sltNian.options[0].value - 1), 0); while(sltNian.options.length < sltNian.selectedIndex + 11) sltNian.options.add(new Option(sltNian.lastChild.value - 0 + 1 + "年", sltNian.lastChild.value - 0 + 1)); } var sltYue = sltNian.cloneNode(); for(var i = 0; i < 12;) { var opt = new Option(++i + "月", i-1); sltYue.options.add(opt); } sltYue.onchange = function() { BuildDateFace(new Date(sltNian.value, sltYue.value)); } sltNian.onchange = function() { sltYue.onchange(); InitYearSelect(); } sltYue.value = today.getMonth(); sltNian.value = today.getFullYear(); tr.cells[0].appendChild(sltNian); tr.cells[0].appendChild(sltYue); tr.cells[0].colSpan = 7; tr.cells[0].align = "center"; var tBody = this.Tag.tBodies[0]; tBody.insertRow(); for(var i = 0; i < 7; i++) { var td = tBody.rows[1].insertCell(); td.align = "center"; td.appendChild(doc.createTextNode("日一二三四五六".charAt(i))); } tBody.rows[1].cells[0].style.color = "red"; tBody.rows[1].cells[6].style.color = "green"; for(var i = 0; i < 6; i++) { var row = tBody.rows[1].cloneNode(true); tBody.appendChild(row); IRow.apply(row); } tBody.rows[7].deleteCell(); tBody.rows[7].cells[5].colSpan = 2; tBody.rows[7].cells[5].lastChild.data = "清空"; tBody.rows[1].style.fontWeight = "bold"; function IRow() { for(var i = 0; i < this.cells.length; i++) { this.cells[i].onmouseover = function() { if(!IsToday(this.title)) this.bgColor = _Kalendar.CapBack; } this.cells[i].onmouseout = function() { if(!IsToday(this.title)) this.bgColor = ""; } this.cells[i].onclick = function() { _Kalendar.Parent.Owner.value = this.title; _Kalendar.Parent.Tag.style.display = "none"; } } } function IsToday(date) { date = date.split("-"); var year = parseInt(date[0]); var month = parseInt(date[1], 10) - 1; date = parseInt(date[2], 10); return today.getFullYear() == year && today.getMonth() == month && today.getDate() == date; } function BuildDateFace(date) { date.setDate(1); var year = date.getFullYear(); var month = date.getMonth(); var offset = date.getDay(); var maxDate = GetDates(year, month); for(var i = 0; i < maxDate; i++) { var td = _Kalendar.Tag.cells[i + 8 + offset]; td.lastChild.data = i + 1; td.title = [year, (month < 9 ? "0" : "") + (month + 1), (i < 9 ? "0" : "") + (i + 1)].join("-"); td.bgColor = (IsToday(td.title)) ? _Kalendar.CapBack : ""; td.disabled = false; } for(var i = 8 + offset + maxDate; i < _Kalendar.Tag.cells.length - 1; i++) { var td = _Kalendar.Tag.cells[i]; td.title = ""; td.lastChild.data = i - 7 - offset - maxDate; td.disabled = true; } lastMonth = GetDates(year, month - 1); while(offset) { var td = _Kalendar.Tag.cells[8 + (--offset)]; td.title = ""; td.lastChild.data = lastMonth - date.getDay() + offset + 1; td.disabled = true; } } function GetDates(year, month) { if(month < 0) { month = 11; year--; } if(month == 12) { month = 0; year++; } var date = new Date(year, month, 31); return 31 - date.getDate() || 31; } } /* 選擇面板類 this.Tag = doc.createElement("div"); // 面板HTML標籤 this.Owner = null; // 面板對應的輸入框。 this.Width = 0; // 寬度,為0時自適應 this.Height = 0; // 面板高度,為0時自適應 this.BorderColor = "#1095c6"; // 邊框顏色 this.BgColor = "white"; // 面板背景顏色 this.Create(container) // 將面板建立到container內 this.Apply(target) // 將面板應用到目標控制項。 */ function SelectPanel(win) { if(!win) win = self; var doc = win.document; var _SelectPannel = this; this.Owner = null; this.Tag = doc.createElement("div"); this.Width = 0; this.Height = 0; this.BorderColor = "#1095c6"; this.BgColor = "white"; IMouseEvent.apply(this.Tag); if(!win._PannelList) win._PannelList = [this]; else win._PannelList.push(this); if(!doc.onmousedown) doc.onmousedown = function() { for(var i in win._PannelList) { if(!win._PannelList[i].AllowShow && win._PannelList[i].Tag.style.display != "none" && doc.activeElement.tagName != "SELECT") win._PannelList[i].Tag.style.display = "none"; } } this.Create = function(container) { var style = this.Tag.style; if(this.Width) style.width = this.Width; if(this.Height) style.height = this.Height; style.backgroundColor = this.BgColor; style.border = "1px solid " + this.BorderColor; style.position = "absolute"; style.left = "0px"; style.top = "0px"; style.overflow = "auto"; style.display = "none"; (container || doc.body).appendChild(this.Tag); } this.Apply = function(target) { target.onclick = function() { _SelectPannel.Owner = this; IMouseEvent.apply(target); var x = target.offsetLeft; var y = target.offsetTop + target.offsetHeight + 1; var sender = target; while(sender.offsetParent) { sender = sender.offsetParent; x += sender.offsetLeft; y += sender.offsetTop; } _SelectPannel.Tag.style.left = x + "px"; _SelectPannel.Tag.style.top = y + "px"; _SelectPannel.Tag.style.display = "block"; } } function IMouseEvent() { this.onmouseenter = function() { _SelectPannel.AllowShow = true; } this.onmouseleave = function() { _SelectPannel.AllowShow = false; } } } </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

做得比較急,所以沒考慮相容性。
通過兩個類實現,一個是面板類,一個是日曆類。
由於我開發時所有公用js都是在最上層視窗一次載入的,所以在子視窗建立對象時需要把當前視窗對象傳進去,例如: var panel = new parent.parent.SelectPanel(self);如果沒傳self參數,預設就是載入js的視窗。

marcian 在網上找的那個西曆演算法有點複雜,呵呵。我擷取當前月最大天數,以及當前月第一天是星期幾是直接通過JS內建的Date函數來實現的。
複製代碼 代碼如下:

// 擷取當月最大天數
//asfman提供更簡單的方式:return (new Date(y, m+1, 0)).getDate()
function GetDates(year, month)
{
    var date = new Date(year, month, 31);
    return 31 - date.getDate() || 31;
}
// 擷取當月第一天是星期幾
function GetFirstDay(year, month)
{
    return (new Date(year, month, 1)).getDay();
} 

/* 下面這部分可以不要,因為即使出現new Date(2007, -1, 31),這種現象,Date會自動轉換成Date(2006, 12, 31)
    if(month < 0)
    {
        month = 11;
        year--;
    }
    if(month == 12)
    {
        month = 0;
        year++;
    }*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.