当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 如何使用jquery快速写一个万年历

如何使用jquery快速写一个万年历

2019年03月21日  | 移动技术网IT编程  | 我要评论

代码如下:


<!doctype html><html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=content-type content="text/html;charset=utf-8">
<style>
.main{
width:600px;
height:350px;
background:gray;
margin-left: auto;
margin-right: auto;
overflow:hidden;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
}
.title{
text-align:center;
}
.date{
float:left;
padding-left:31px;
}
.date1{
float:left;
width:20px;
height:20px;
padding-top:10px;
padding-left:30px;
padding-right:30px;
}
.content{
margin-left:25px;
}
</style>
<script type="text/javascript" src="jquery.min.js"></script>
<script>
function gettime(year,month,day){
y = year
m = month
d = day
var mydate = new date(year,month-1,day);
return mydate;
}
function days_in_month(year, month) {
y = year;
m = month;
return 32 - new date(y, m - 1, 32).getdate();
}
function view(year,month){

var w = gettime(year,month,1).getday()-1;
var num = days_in_month(year,month);
var index = 1;
//console.log(w);
var data = new array();
for(var d = 0; d < num+w; d++){
if(d<w){
data[d] = '';
}else{
data[d] = index;
index++;
}
}
$("#content").html('');
for(var k =0;k < data.length;k++){
if(k%7==0){
$("#content").append("<p id='t"+k+"' class='date1'>"+data[k]+"</p><br>");
}else{
$("#content").append("<p id='t"+k+"' class='date1'>"+data[k]+"</p>");
}
}
$("#content > p").mouver(function(){
if($(this).text()!=''){
$(this).css('background','red');
}
});
$("#content > p").mouseout(function(){
if($(this).text()!=''){
$(this).css('background','gray');
}
});
}

$(document).ready(function (){
for(var t = 1970; t < 2999; t++){
$("#yearsel").append("<option value ='"+t+"'>"+t+"</option>");
}
for(var y = 1; y < 13;y++){
$("#monthsel").append("<option value ='"+y+"'>"+y+"</option>");
}
var year = new date().getfullyear();
var month = new date().getmonth()+1;
var day = new date().getdate();
var w = gettime(year,month,1).getday()-1;
var num = day + w -1;
$("#yearsel").change(function(){
year = $("#yearsel option:selected").text();
month = $("#monthsel option:selected").text();
view(year,month);
});
$("#monthsel").change(function(){
year = $("#yearsel option:selected").text();
month = $("#monthsel option:selected").text();
view(year,month);
});
var odate = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日',];
for(var i = 0;i < 7;i++){
$("#title").append("<p class='date'><b>"+odate[i]+"</b></p>");
}
$("#yearsel option[value='"+year+"']").attr("selected", true);
view(year,month);
//标记当前日期
$("#t"+num).css('background','yellow');
});
</script>
</head>
<body>
<p id="main" class="main">
<center><h3>万年历</h3></center>
<select id="yearsel">
</select>年
<select id="monthsel">
</select>月<br><br>
<p id="title" class="title">
</p>
<p id="content" class="content">
</p>
</p>
</body>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网