1 日历
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jquery练习02日历插件</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
<style type="text/css">
#div_date{
width:350px;
border:1px solid blue;
padding:2px;
margin-top:10px;
text-align: center;
font-weight: bold;
}
#tab_date{
border:1px solid blue;
width:345px;
border-collapse: collapse;
margin:2px auto;
}
#tab_date th,#tab_date td{
text-align: center;
border:1px solid blue;
padding:4px;
}
#tab_date th{
background: #cccccc;
color:blue;
}
</style>
</head>
<body>
<input type="date" name="birth"/><br/>
<input type="text" name="birth" id="input_date"/>
<script type="text/javascript">
var date=new Date();
var year=date.getFullYear(),month=date.getMonth()+1;
$(function(){
$("#input_date").focus(function(){
$("#div_date").remove();
$(this).after("<div id='div_date'></div>");
createTable(date);
});
$("#input_date").blur(function(){
});
});
function createTable(date){
var $tab=$("<table id='tab_date'></table>");
var $tr1=$("<tr><th colspan='7'></th></tr>");
$tr1.children("th:first").append("<input type='button' value='<<' id='but_year_reduce'/> ");
$tr1.children("th:first").append("<input type='button' value='<' id='but_month_reduce'/> ");
$tr1.children("th:first").append("<input type='text' id='text_date'/> ");
$tr1.children("th:first").append("<input type='button' value='>' id='but_month_add'/> ");
$tr1.children("th:first").append("<input type='button' value='>>' id='but_year_add'/>");
var weekStr="日一二三四五六";
var $tr2=$("<tr></tr>");
for (var i = 0; i < weekStr.length; i++) {
$tr2.append("<th>"+(weekStr.charAt(i))+"</th>");
}
$tab.append($tr1); $tab.append($tr2);
var daysTotal=getMyDays(year,month);
var n=0;
for(var i=1;true;i++){
var $tr=$("<tr></tr>");
for(var j=0;j<=6;j++){
var $td=$("<td></td>");
if((n==0&&i==1&&j==getFirstDayWeek(year,month))||(n!=0)){
n++;
}
if(n!=0&&n<=daysTotal){
$td.text(n);
}
$tr.append($td);
}
$tab.append($tr);
if(n>=daysTotal){
break;
}
}
$("#div_date").append($tab);
$("#text_date").attr("disabled","disabled");
$("#text_date").css({"width":"100px","text-align":"center"});
$("#text_date").val(year+"年"+month+"月");
$("#tab_date tr:gt(1) td:parent").hover(function(){
$(this).css("background-color","#cc6699");
},function(){
$(this).removeAttr("style");
});
$("#tab_date tr:gt(1) td:parent").bind("click",function(){
$(this).css("background-color","#aaaaaa");
date.setDate($(this).text());
$("#input_date").val(year+"年"+month+"月"+date.getDate()+"日");
});
}
function getMyDays(year,month){
switch(month){
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return ((year%400==0)||(year%4==0&&year%100!=0))?29:28;
default:
return 31;
}
}
function getFirstDayWeek(year,month){
var dateFirst=new Date(year,month-1,1);
return dateFirst.getDay();
}
</script>
</body>
</html>