下面記錄一下對經典 Calendar 日曆控件的改造,使其能夠兼容 IE9、谷歌與火狐瀏覽器。目前僅做到能用,彈出位置略有差異,歡迎高手 further 完善。
原代碼:
this.iframe = window.frames("meizzCalendarIframe");
改為:
this.iframe = window.frames["meizzCalendarIframe"];
此處原文使用了全角括號,導致在標準瀏覽器中報錯,改用方括號索引才是正確寫法。
原代碼:
var a = (arguments.length == 0) ? WebCalendar.day[this.id.substr(8)].split("/") : arguments[0].split("/");
改為:
var a = WebCalendar.day[this.id.substr(8)].split("/");
原代碼在鼠標移出時用 removeAttribute 移除背景色,這在部分瀏覽器中並不生效,改為先記錄原背景色再恢復的方式:
var bgbgbg;
function dayMouseOver() {
this.className = "over";
bgbgbg = this.style.backgroundColor;
this.style.backgroundColor = WebCalendar.darkColor;
if (WebCalendar.day[this.id.substr(8)].split("/")[1] == WebCalendar.thisMonth)
this.style.color = WebCalendar.lightColor;
}
function dayMouseOut() {
this.className = "out";
var d = WebCalendar.day[this.id.substr(8)], a = d.split("/");
this.style.backgroundColor = bgbgbg;
if (a[1] == WebCalendar.thisMonth && d != WebCalendar.today) {
if (WebCalendar.dateStyle && a[0] == parseInt(WebCalendar.dateStyle[4], 10))
this.style.color = WebCalendar.lightColor;
this.style.color = WebCalendar.wordColor;
}
}
這幾處改動主要圍繞著標準瀏覽器對 DOM 與樣式 API 的更嚴格實現展開:全角符號、removeAttribute 對行內樣式的處理差異,都是老舊控件在新瀏覽器中失效的常見原因。逐一修正後,日曆控件便能在 IE9、Chrome、Firefox 上正常顯示與交互了。