<img src="https://img-blog.csdn.net/20160929163544995?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=640,user-scalable=no"/>
<title>申请成为坊主</title>
<style>
*{margin:0;padding:0;list-style:none;border:0;}
.form1{padding:100px;float:left}
.yearout{float:left;position:relative;width:100px;text-align:center;margin-right:25px;}
.yearhead,.monthhead,.dayhead{width:100%;border:1px solid #ccc;line-height:30px;cursor:pointer;}
.years,.months,.days{width:100%;border:1px solid #ccc;border-top:0;line-height:30px;cursor:pointer;max-height:300px;overflow-y:auto;display:none;}
.years div:hover,.months div:hover,.days div:hover{background:red}
.months{float:left;position:relative;}
.days{float:left;position:relative;}
</style>
</head>
<body>
<div class="form1">
<div class="yearout">
<div class="yearhead">2015</div>
<div class="years">
</div>
</div>
<div class="yearout">
<div class="monthhead">2015</div>
<div class="months">
</div>
</div>
<div class="yearout">
<div class="dayhead">2015</div>
<div class="days">
</div>
</div>
</div>
<div class="form1">
<div class="yearout">
<div class="yearhead">2015</div>
<div class="years">
</div>
</div>
<div class="yearout">
<div class="monthhead">2015</div>
<div class="months">
</div>
</div>
<div class="yearout">
<div class="dayhead">2015</div>
<div class="days">
</div>
</div>
</div>
<div class="form1">
<div class="yearout">
<div class="yearhead">2015</div>
<div class="years">
</div>
</div>
<div class="yearout">
<div class="monthhead">2015</div>
<div class="months">
</div>
</div>
<div class="yearout">
<div class="dayhead">2015</div>
<div class="days">
</div>
</div>
</div>
<script src="jquery.min.js"></script>
<!--/*var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})
console.log(result)*/-->
<script>
$(function(){
var D1=new DateTime();
D1.init('.form1',{//最外层div
yearhead:'.yearhead',//选定的年
monthhead:'.monthhead',//选定的月
dayhead:'.dayhead',//选定的日
year:'.years',//年容器
month:'.months',//月容器
day:'.days'//日容器
});
$('.yearout').each(function(index, element) {//点击显示
$(element).click(function(e) {
$(element).find('div:eq('+1+')').show();
$(element).siblings('.yearout').find('div:eq('+1+')').hide();
});
});
})
function DateTime(){
this.set={
yearhead:'.yearhead',//选定的年
monthhead:'.monthhead',//选定的月
dayhead:'.dayhead',//选定的日
year:'.years',//年容器
month:'.months',//月容器
day:'.days'//日容器
}
}
DateTime.prototype.init=function(obj1,obj2){
var This=this;
this.obj=$(obj1);
$.extend(This.set,obj2);
this.obj.each(function(index,element){
var myDate = new Date();
var nowyear=myDate.getFullYear();//当前年份
var nowmonth=myDate.getMonth()+1;//当前月份
var nowday=myDate.getDate();//当前几号
var secyear=nowyear;//选择年份
var secmonth=nowmonth;//选择月份
var secday=nowday;//选择日
var nums=[28,29,30,31];
years()
months()
days()
setdatetime({
nowyear:nowyear+"年",
nowmonth:nowmonth+"月",
nowday:nowday+"日"
});//设置最初始的时间
function setdatetime(times){//设置选中的当前时间
$(element).find(This.set.yearhead).html(times.nowyear)
$(element).find(This.set.monthhead).html(times.nowmonth)
$(element).find(This.set.dayhead).html(times.nowday)
}
$(document).on('click',obj1+':eq('+index+') '+This.set.year+' div',function(e) {
secyear=$(this).html()
var re = /年|月|日/g
secyear=secyear.replace(re,'');
$(this).parent(This.set.year).hide()
days();
setdatetime({
nowyear:secyear+"年"
});
});
$(document).on('click',obj1+':eq('+index+') '+This.set.month+' div',function(e) {
secmonth=$(this).html();
var re = /年|月|日/g
secmonth=secmonth.replace(re,'');
$(this).parent(This.set.month).hide()
days();
setdatetime({
nowmonth:secmonth+"年"
});
});
$(document).on('click',obj1+':eq('+index+') '+This.set.day+' div',function(e) {
secday=$(this).html();
var re = /年|月|日/g
secday=secday.replace(re,'');
$(this).parent(This.set.day).hide()
setdatetime({
nowday:secday+"日"
});
});
function years(){
for(i=2015;i>=2001;i--){
if(i==nowyear){
$(element).find(This.set.year).append('<div selected>'+i+'年</div>');
}else{
$(element).find(This.set.year).append('<div>'+i+'年</div>');
}
}
}
function months(){
for(i=12;i>=1;i--){
if(i==nowmonth){
$(element).find(This.set.month).append('<div>'+i+'月</div>');
}else{
$(element).find(This.set.month).append('<div>'+i+'月</div>');
}
}
}
function days(){
$(element).find(This.set.day).html('');//清空原有数据
var day_num=nums[3]
//This.runnian(secyear)&&secmonth==2?day_num=nums[1]:day_num=day_num
if(This.runnian(secyear)&&secmonth){
day_num=nums[1]
}
if((secmonth==1||secmonth==3||secmonth==5||secmonth==7||secmonth==8||secmonth==10||secmonth==12)){
day_num=nums[3];
}
if((secmonth==4||secmonth==6||secmonth==9||secmonth==11)){
day_num=nums[2];
}
if(!This.runnian(secyear)&&secmonth==2){
day_num=nums[0];
}
setdatetime({
nowmonth:12+"月",
nowday:day_num+"日"
});
for(i=day_num;i>=1;i--){
if(i==nowday){
$(element).find(This.set.day).append('<div selected>'+i+'日</div>');
}else{
$(element).find(This.set.day).append('<div>'+i+'日</div>');
}
}
}
})
}
DateTime.prototype.runnian=function(secyear){//判断是否闰年
return (secyear%4==0&&(secyear%10!=0||secyear%400==0))
}
</script>
</body>
</html>
转载请注明出处:http://blog.csdn.net/sllailcp/article
预览图如下图,默认为当天日期,2016年9月29.