<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>示例</title>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
var flag=1;
$('table td').click(function(){
if(flag==1){
$(this).addClass('AAA');
flag=0;
alert('你點選的是'+$(this).text()+" 此時的已經flag是:"+flag) ;
}else{
$('table td').removeClass('AAA');
flag=1;
alert('你點選的是'+$(this).text()+" 此時的已經flag是:"+flag) ;
}
})
})
</script>
</head>
<body>
<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);
}
}
}
Object.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結構)
this.Days = []; //日期物件列表
this.SetOptions(options);
this.Year = this.options.Year;
this.Month = this.options.Month;
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();
},
//設定預設屬性
SetOptions: function(options) {
this.options = { //預設值
Year: new Date().getFullYear(), //顯示年
Month: new Date().getMonth() + 1, //顯示月
SelectDay: null, //選擇日期
onSelectDay: function() {}, //在選擇日期觸發
onToday: function() {}, //在當天日期觸發
onFinish: function() {} //日曆畫完後觸發
};
Object.extend(this.options, options || {});
},
//上一個月
PreMonth: function() {
//先取得上一個月的日期物件
var d = new Date(this.Year, this.Month - 2, 1);
//再設定屬性
this.Year = d.getFullYear();
this.Month = d.getMonth() + 1;
//重新畫日曆
this.Draw();
},
//下一個月
NextMonth: function() {
var d = new Date(this.Year, this.Month, 1);
this.Year = d.getFullYear();
this.Month = d.getMonth() + 1;
this.Draw();
},
//畫日曆
Draw: function() {
//用來儲存日期列表
var arr = [];
//用當月第一天在一週中的日期值作為當月離第一天的天數
for (var i = 1, firstDay = new Date(this.Year, this.Month - 1, 1).getDay(); i <= firstDay; i++) {
arr.push(" ");
}
//用當月最後一天在一個月中的日期值作為當月的天數
for (var i = 1, monthDay = new Date(this.Year, this.Month, 0).getDate(); i <= monthDay; i++) {
arr.push(i);
}
var frag = document.createDocumentFragment();
this.Days = [];
while (arr.length > 0) {
//每個星期插入一個tr
var row = document.createElement("tr");
//每個星期有7天
for (var i = 1; i <= 7; i++) {
var cell = document.createElement("td");
cell.innerHTML = " ";
if (arr.length > 0) {
var d = arr.shift();
cell.innerHTML = d;
if (d > 0) {
this.Days[d] = cell;
//判斷是否今日
if (this.IsSame(new Date(this.Year, this.Month - 1, d), new Date())) {
this.onToday(cell);
}
//判斷是否選擇日期
if (this.SelectDay && this.IsSame(new Date(this.Year, this.Month - 1, d), this.SelectDay)) {
this.onSelectDay(cell);
}
}
}
row.appendChild(cell);
}
frag.appendChild(row);
}
//先清空內容再插入(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: 20px;
background-color: pink; //設定背景色
text-align: left;
margin: 0 auto;
width: 90%;
height: 620px;
padding: 10px;
line-height: 1.5em;
border: 1px solid red;
color: black;
}
.Calendar a {
color: #1e5494;
}
.Calendar table {
width: 100%;
border: 1px;
border-collapse: collapse; //相鄰的相鄰邊被合併
}
.Calendar table thead {
color: blueviolet;
}
.Calendar table tr {
height: 90px;
}
.Calendar table th {
border: 1px solid blue;
font-size: 11px;
padding: 1px;
font-size: 30px; //設定表格字型大小
}
.Calendar table td {
border: 1px solid blue;
font-size: 11px;
padding: 1px;
font-size: 30px; //設定表格字型大小
}
.Calendar table tr.tou {
height: 40px;
text-align: center;
}
#idCalendarPre {
cursor: pointer;
float: left;
padding-right: 5px;
}
#toyear {
color: green;
font-size: 29px;
text-align: center;
padding-bottom: 15px;
}
#idCalendarNext {
cursor: pointer;
float: right;
padding-right: 5px;
}
#idCalendar {
font-size: 34px;
}
#idCalendar td.onToday {
//今天的顏色
font-weight: bold;
color: red;
background: blue;
}
#idCalendar td.onSelect {
//要選擇的顏色
font-weight: bold;
color: blue;
}
.AAA {
background-color: green;
}
</style>
<div class="Calendar">
<div id="idCalendarPre"><<</div>
<div id="idCalendarNext">>></div>
<div id="toyear"><span id="idCalendarYear">2008</span>年 <span id="idCalendarMonth">8</span>月</div>
<table border="1px" cellpadding="0" cellspacing="0">
<thead>
<tr class="tou">
<th>星期日</th>
<th>星期一</th>
<th>星期二</th>
<th>星期三</th>
<th>星期四</th>
<th>星期五</th>
<th>星期六</th>
</tr>
</thead>
<tbody id="idCalendar">
</tbody>
</table>
</div>
<script language="JavaScript">
var cale = new Calendar("idCalendar", {
SelectDay: new Date().setDate(10), //要選擇的日期,可註釋
// onSelectDay: function(o){ o.className = "onSelect"; }, //增加 類
onToday: function(o) {
o.className = "onToday";
},
onFinish: function() {
$$("idCalendarYear").innerHTML = this.Year;
$$("idCalendarMonth").innerHTML = this.Month; //表頭年份
// var flag = [10,15,20]; //判斷選擇是不是指定日期
// for(var i = 0, len = flag.length; i < len; i++){
// this.Days[flag[i]].innerHTML = "<a href='javascript:void(0);' onclick=\"alert('日期是:"+this.Year+"/"+this.Month+"/"+flag[i]+"');return false;\">" + flag[i] + "</a>";
// }
}
});
$$("idCalendarPre").onclick = function() {
cale.PreMonth();
}
$$("idCalendarNext").onclick = function() {
cale.NextMonth();
}
//遍歷表格並且列印出表格的值
var tb = document.getElementById('idCalendar');
var arr = new Array(); //遍歷表格
var z = 0;
for (var i = 0; i < tb.rows.length; i++) {
for (var j = 0; j < tb.rows[i].cells.length; j++) {
// alert(tb.rows[i].cells[j].innerText); //列印所有的表格元素
if (tb.rows[i].cells[j].innerHTML != " ") {
arr[z++] = tb.rows[i].cells[j].innerText;
}
}
}
// alert(arr); //列印所有的表格元素除過空
//點選表格裡面的值
/* var A=0;
tb.onclick = function(e) { //給日期格子新增事件
// alert(e.target.innerHTML+":"+e.target.innerText); //注意產生的值
// alert(e.target.innerHTML); //輸出滑鼠所點選的格子裡面的值
// alert(typeof e.target.innerHTML); //看一下輸出是什麼型別
// if (e.target.innerHTML == " ") alert('空');
// if (e.target.innerHTML !== " ") {
// alert(e.target.innerHTML + ":" + e.target.nodeName);
if (e.target.innerText != 0) { //如果值不為0
if (A%10==0) {
e.target.style.backgroundColor = 'red'; //點選誰誰變紅
e.target.style.color = 'blue';
alert(++A);
}else{
e.target.style.backgroundColor = 'pink'; //點選誰誰變紅
e.target.style.color = 'black';
alert(++A);
}
}
} */
</SCRIPT>
</body>
</html>