一個(gè)js寫(xiě)的日歷(代碼部分網(wǎng)摘)
更新時(shí)間:2009年09月20日 18:56:14 作者:
這段代碼忘記從哪里摘錄的了,剛開(kāi)始摘錄的時(shí)候,水平有限,有些地方的寫(xiě)法沒(méi)看懂;經(jīng)過(guò)學(xué)習(xí),現(xiàn)在終于能明白其中的含義了。
特意貼出這段代碼,因?yàn)樗拇a簡(jiǎn)潔和清晰,覺(jué)得不錯(cuò),供大家分享。
×××××××函數(shù)定義部分
<script type="text/javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
var Calendar = Class.create();
Calendar.prototype = {
initialize: function(container, options) {
this.Container = $(container);//容器(table結(jié)構(gòu))
this.Days = [];//日期對(duì)象列表
this.SetOptions(options);
this.Year = this.options.Year || new Date().getFullYear();
this.Month = this.options.Month || new Date().getMonth() + 1;
this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null;
this.onSelectDay = this.options.onSelectDay;
this.onToday = this.options.onToday;
this.onFinish = this.options.onFinish;
this.Draw();
},
//設(shè)置默認(rèn)屬性
SetOptions: function(options) {
this.options = {//默認(rèn)值
Year: 0,//顯示年
Month: 0,//顯示月
SelectDay: null,//選擇日期
onSelectDay: function(){},//在選擇日期觸發(fā)
onToday: function(){},//在當(dāng)天日期觸發(fā)
onFinish: function(){}//日歷畫(huà)完后觸發(fā)
};
Extend(this.options, options || {});
},
//當(dāng)前月
NowMonth: function() {
this.PreDraw(new Date());
},
//上一月
PreMonth: function() {
this.PreDraw(new Date(this.Year, this.Month - 2, 1));
},
//下一月
NextMonth: function() {
this.PreDraw(new Date(this.Year, this.Month, 1));
},
//上一年
PreYear: function() {
this.PreDraw(new Date(this.Year - 1, this.Month - 1, 1));
},
//下一年
NextYear: function() {
this.PreDraw(new Date(this.Year + 1, this.Month - 1, 1));
},
//根據(jù)日期畫(huà)日歷
PreDraw: function(date) {
//再設(shè)置屬性
this.Year = date.getFullYear(); this.Month = date.getMonth() + 1;
//重新畫(huà)日歷
this.Draw();
},
//畫(huà)日歷
Draw: function() {
//用來(lái)保存日期列表
var arr = [];
//用當(dāng)月第一天在一周中的日期值作為當(dāng)月離第一天的天數(shù)
for(var i = 1, firstDay = new Date(this.Year, this.Month - 1, 1).getDay(); i <= firstDay; i++){ arr.push(0); }
//用當(dāng)月最后一天在一個(gè)月中的日期值作為當(dāng)月的天數(shù)
for(var i = 1, monthDay = new Date(this.Year, this.Month, 0).getDate(); i <= monthDay; i++){ arr.push(i); }
//清空原來(lái)的日期對(duì)象列表
this.Days = [];
//插入日期
var frag = document.createDocumentFragment();
while(arr.length){
//每個(gè)星期插入一個(gè)tr
var row = document.createElement("tr");
//每個(gè)星期有7天
for(var i = 1; i <= 7; i++){
var cell = document.createElement("td"); cell.innerHTML = " ";
if(arr.length){
var d = arr.shift();
if(d){
cell.innerHTML = d;
this.Days[d] = cell;
var on = new Date(this.Year, this.Month - 1, d);
//判斷是否今日
this.IsSame(on, new Date()) && this.onToday(cell);
//判斷是否選擇日期
this.SelectDay && this.IsSame(on, this.SelectDay) && this.onSelectDay(cell);
}
}
row.appendChild(cell);
}
frag.appendChild(row);
}
//先清空內(nèi)容再插入(ie的table不能用innerHTML)
while(this.Container.hasChildNodes()){ this.Container.removeChild(this.Container.firstChild); }
this.Container.appendChild(frag);
//附加程序
this.onFinish();
},
//判斷是否同一日
IsSame: function(d1, d2) {
return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate());
}
}
</script>
××××××××樣式表的定義
<style type="text/css">
.Calendar {
font-family:Verdana;
font-size:12px;
background-color:#e0ecf9;
text-align:center;
width:400px;
height:180px;
padding:10px;
line-height:1.5em;
}
.Calendar a{
color:#1e5494;
}
.Calendar table{
width:100%;
border:0;
}
.Calendar table thead{color:#acacac;}
.Calendar table td {
font-size: 11px;
padding:1px;
}
#idCalendarPre{
cursor:pointer;
float:left;
padding-right:5px;
}
#idCalendarNext{
cursor:pointer;
float:right;
padding-right:5px;
}
#idCalendar td.onToday {
font-weight:bold;
color:#C60;
}
#idCalendar td.onSelect {
font-weight:bold;
}
#buttonposition{
margin-left: 10%;
}
</style>
***********下面是應(yīng)用的主體部分
<div class="Calendar">
<div id="idCalendarPre"><<</div>
<div id="idCalendarNext">>></div>
<span id="idCalendarYear"></span>年 <span id="idCalendarMonth"></span>月
<table cellspacing="0">
<thead>
<tr>
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
</thead>
<tbody id="idCalendar">
</tbody>
</table>
</div>
<div id="buttonposition" ><!-- 通過(guò)id獲取樣式 -->
<input id="idCalendarPreYear" type="button" class="bt" value="上一年" />
<input id="idCalendarNow" type="button" class="bt" value="當(dāng)前月" />
<input id="idCalendarNextYear" type="button" class="bt" value="下一年" />
</div>
<script language="JavaScript">
var cale = new Calendar("idCalendar", {
onToday: function(o){ o.className = "onToday"; },
onFinish: function(){
$("idCalendarYear").innerHTML = this.Year; $("idCalendarMonth").innerHTML = this.Month;
}
});
$("idCalendarPre").onclick = function(){ cale.PreMonth(); }
$("idCalendarNext").onclick = function(){ cale.NextMonth(); }
$("idCalendarPreYear").onclick = function(){ cale.PreYear(); }
$("idCalendarNextYear").onclick = function(){ cale.NextYear(); }
$("idCalendarNow").onclick = function(){ cale.NowMonth(); }
</script>
</div>
×××××××函數(shù)定義部分
復(fù)制代碼 代碼如下:
<script type="text/javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
var Calendar = Class.create();
Calendar.prototype = {
initialize: function(container, options) {
this.Container = $(container);//容器(table結(jié)構(gòu))
this.Days = [];//日期對(duì)象列表
this.SetOptions(options);
this.Year = this.options.Year || new Date().getFullYear();
this.Month = this.options.Month || new Date().getMonth() + 1;
this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null;
this.onSelectDay = this.options.onSelectDay;
this.onToday = this.options.onToday;
this.onFinish = this.options.onFinish;
this.Draw();
},
//設(shè)置默認(rèn)屬性
SetOptions: function(options) {
this.options = {//默認(rèn)值
Year: 0,//顯示年
Month: 0,//顯示月
SelectDay: null,//選擇日期
onSelectDay: function(){},//在選擇日期觸發(fā)
onToday: function(){},//在當(dāng)天日期觸發(fā)
onFinish: function(){}//日歷畫(huà)完后觸發(fā)
};
Extend(this.options, options || {});
},
//當(dāng)前月
NowMonth: function() {
this.PreDraw(new Date());
},
//上一月
PreMonth: function() {
this.PreDraw(new Date(this.Year, this.Month - 2, 1));
},
//下一月
NextMonth: function() {
this.PreDraw(new Date(this.Year, this.Month, 1));
},
//上一年
PreYear: function() {
this.PreDraw(new Date(this.Year - 1, this.Month - 1, 1));
},
//下一年
NextYear: function() {
this.PreDraw(new Date(this.Year + 1, this.Month - 1, 1));
},
//根據(jù)日期畫(huà)日歷
PreDraw: function(date) {
//再設(shè)置屬性
this.Year = date.getFullYear(); this.Month = date.getMonth() + 1;
//重新畫(huà)日歷
this.Draw();
},
//畫(huà)日歷
Draw: function() {
//用來(lái)保存日期列表
var arr = [];
//用當(dāng)月第一天在一周中的日期值作為當(dāng)月離第一天的天數(shù)
for(var i = 1, firstDay = new Date(this.Year, this.Month - 1, 1).getDay(); i <= firstDay; i++){ arr.push(0); }
//用當(dāng)月最后一天在一個(gè)月中的日期值作為當(dāng)月的天數(shù)
for(var i = 1, monthDay = new Date(this.Year, this.Month, 0).getDate(); i <= monthDay; i++){ arr.push(i); }
//清空原來(lái)的日期對(duì)象列表
this.Days = [];
//插入日期
var frag = document.createDocumentFragment();
while(arr.length){
//每個(gè)星期插入一個(gè)tr
var row = document.createElement("tr");
//每個(gè)星期有7天
for(var i = 1; i <= 7; i++){
var cell = document.createElement("td"); cell.innerHTML = " ";
if(arr.length){
var d = arr.shift();
if(d){
cell.innerHTML = d;
this.Days[d] = cell;
var on = new Date(this.Year, this.Month - 1, d);
//判斷是否今日
this.IsSame(on, new Date()) && this.onToday(cell);
//判斷是否選擇日期
this.SelectDay && this.IsSame(on, this.SelectDay) && this.onSelectDay(cell);
}
}
row.appendChild(cell);
}
frag.appendChild(row);
}
//先清空內(nèi)容再插入(ie的table不能用innerHTML)
while(this.Container.hasChildNodes()){ this.Container.removeChild(this.Container.firstChild); }
this.Container.appendChild(frag);
//附加程序
this.onFinish();
},
//判斷是否同一日
IsSame: function(d1, d2) {
return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate());
}
}
</script>
××××××××樣式表的定義
復(fù)制代碼 代碼如下:
<style type="text/css">
.Calendar {
font-family:Verdana;
font-size:12px;
background-color:#e0ecf9;
text-align:center;
width:400px;
height:180px;
padding:10px;
line-height:1.5em;
}
.Calendar a{
color:#1e5494;
}
.Calendar table{
width:100%;
border:0;
}
.Calendar table thead{color:#acacac;}
.Calendar table td {
font-size: 11px;
padding:1px;
}
#idCalendarPre{
cursor:pointer;
float:left;
padding-right:5px;
}
#idCalendarNext{
cursor:pointer;
float:right;
padding-right:5px;
}
#idCalendar td.onToday {
font-weight:bold;
color:#C60;
}
#idCalendar td.onSelect {
font-weight:bold;
}
#buttonposition{
margin-left: 10%;
}
</style>
***********下面是應(yīng)用的主體部分
復(fù)制代碼 代碼如下:
<div class="Calendar">
<div id="idCalendarPre"><<</div>
<div id="idCalendarNext">>></div>
<span id="idCalendarYear"></span>年 <span id="idCalendarMonth"></span>月
<table cellspacing="0">
<thead>
<tr>
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
</thead>
<tbody id="idCalendar">
</tbody>
</table>
</div>
<div id="buttonposition" ><!-- 通過(guò)id獲取樣式 -->
<input id="idCalendarPreYear" type="button" class="bt" value="上一年" />
<input id="idCalendarNow" type="button" class="bt" value="當(dāng)前月" />
<input id="idCalendarNextYear" type="button" class="bt" value="下一年" />
</div>
<script language="JavaScript">
var cale = new Calendar("idCalendar", {
onToday: function(o){ o.className = "onToday"; },
onFinish: function(){
$("idCalendarYear").innerHTML = this.Year; $("idCalendarMonth").innerHTML = this.Month;
}
});
$("idCalendarPre").onclick = function(){ cale.PreMonth(); }
$("idCalendarNext").onclick = function(){ cale.NextMonth(); }
$("idCalendarPreYear").onclick = function(){ cale.PreYear(); }
$("idCalendarNextYear").onclick = function(){ cale.NextYear(); }
$("idCalendarNow").onclick = function(){ cale.NowMonth(); }
</script>
</div>
您可能感興趣的文章:
- JS顯示日歷和天氣的方法
- 輕量級(jí)的原生js日歷插件calendar.js使用指南
- JS學(xué)習(xí)之一個(gè)簡(jiǎn)易的日歷控件
- 純js簡(jiǎn)單日歷實(shí)現(xiàn)代碼
- js編寫(xiě)當(dāng)天簡(jiǎn)單日歷效果【實(shí)現(xiàn)代碼】
- js日歷控件(可精確到分鐘)
- 很好用的js日歷算法詳細(xì)代碼
- js+html制作簡(jiǎn)單日歷的方法
- JavaScript日歷實(shí)現(xiàn)代碼
- php+javascript的日歷控件
- Vue.js創(chuàng)建Calendar日歷效果
- js實(shí)現(xiàn)簡(jiǎn)單的日歷顯示效果函數(shù)示例
相關(guān)文章
兼容FireFox 的 js 日歷 支持時(shí)間的獲取
應(yīng)網(wǎng)友要求,花了五六個(gè)小時(shí),為我經(jīng)常用的 js 日歷添加時(shí)間的獲取。2009-03-03
超級(jí)可愛(ài)純js網(wǎng)頁(yè)時(shí)鐘
超級(jí)可愛(ài)純js網(wǎng)頁(yè)時(shí)鐘...2007-04-04
JavaScript 判斷日期格式是否正確的實(shí)現(xiàn)代碼
沒(méi)有多大變動(dòng),主要是返回錯(cuò)誤信息,以便調(diào)用函數(shù)部分可以alert出來(lái)。據(jù)說(shuō)可以用正則表達(dá)式校驗(yàn),下次再研究下。2011-07-07
js 模仿網(wǎng)上的限時(shí)搶購(gòu)效果
顯示類(lèi)似還剩 14小時(shí)52分15秒這樣的效果,顯示活動(dòng)剩余多長(zhǎng)時(shí)間就結(jié)束。2010-07-07
javascript 日歷提醒系統(tǒng)( 兼容所有瀏覽器 )
日歷提醒插件(純 javascript + css 打造,不含各類(lèi)添加劑) 代碼比較精簡(jiǎn),數(shù)據(jù)可以從數(shù)據(jù)庫(kù)中讀取。2009-04-04

