當前時間與時期聯動
阿新 • • 發佈:2018-06-26
con cte selected span -s ref tle head image
一些小方法的總結 當前時間的打印與 時間的聯動效果實現
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" type="text/css" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css"/> <script src="public/jquery-3.2.1.min.js"></script> <script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js" type="text/javascript"></script> <title>Document</title> </head> <body> <selectid="nian" onchange="changeTian()"> </select>年 <select id="yue" onchange="changeTian()"> </select>月 <select id="tian"> </select>日 <div id="shijian"> </div> </body> </html> <script type="text/javascript"> nian(); yue(); tian(); showTime(); function showTime(){ var nowTime = new Date(); $("#shijian").html(nowTime.toLocaleString()); setTimeout("showTime()",1000);//每一秒執行一次 } function nian(){ var nowtime = new Date();//獲取系統當前時間 var nian = nowtime.getFullYear();//獲取當前年份 var str = ""; for(var i=1900;i<=2020;i++){ if(i==nian){ str +="<option selected>"+i+"</option>" }else{ str +="<option>"+i+"</option>"; } }$("#nian").html(str); } function yue(){ var myDate = new Date(); var month=myDate.getMonth()+1;//獲取當前月 var str =""; for(var i =1;i<=12;i++){ if(i==month){ str +="<option selected>"+i+"</option>" //選中當前月 }else{ str +="<option>"+i+"</option>"; } } $("#yue").html(str); } function tian(){ var nian = $("#nian").val(); var yue = $("#yue").val(); var ts =30; if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12){ //利用年月來改變天數的多少 ts = 31; }else if(yue==2){ if((nian%4==0&&nian%100 !=0)||nian%400==0){ ts = 29; }else{ ts=28; } } var str =""; for(var i=1;i<=ts;i++){ str+="<option>"+i+"</option>"; } $("#tian").html(str); } function changeTian(){ tian(); } </script>
當前時間與時期聯動