4.10 pm例题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> </style> </head> <body> <!--<span id="s"></span>--> <select id="nian" onclick="Bian()"></select>年 <select id="yue" onclick="Bian()"></select>月 <select id="ri"></select>日 </body> <script type="text/javascript"> /*function S() { var d = new Date(); //年 var n = d.getFullYear(); //月 var y = d.getMonth()+1; //日 var r = d.getDate(); //小时 var x = d.getHours(); //分钟 var m = d.getMinutes(); //秒数 var s = d.getSeconds(); var str = n+"年"+y+"月"+r+"日"+x+"时"+m+"分"+s+"秒"; document.getElementById("s").innerHTML = str; } window.setInterval("S()",0);*/ Nian(); Yue(); Tian(); //补充年份 function Nian() { //获取当前日期 var d = new Date(); var n = parseInt(d.getFullYear()); var str = ""; for(var i=n-5;i<n+6;i++) { if(i==n) { str += "<option selected='selected'>"+i+"</option>"; } else { str += "<option>"+i+"</option>"; } } document.getElementById("nian").innerHTML = str; } //补充月份 function Yue() { var d = new Date(); var y = parseInt(d.getMonth()+1); var str = ""; for(var i=1;i<13;i++) { if(i==y) { str += "<option selected='selected'>"+i+"</option>"; } else { str += "<option>"+i+"</option>"; } } document.getElementById("yue").innerHTML = str; } //补充天数 function Tian() { var d = new Date(); var t = parseInt(d.getDate()); var n = document.getElementById("nian").value; var y = document.getElementById("yue").value; //alert(n); var ts = 31; //30天 if(y==4 || y==6 || y==9 || y==11) { ts = 30; } //29或28 if(y==2) { if((n%4==0 && n%100!=0) || n%400==0) { ts=29; } else { ts=28; } } var str = ""; for(var i=1;i<ts+1;i++) { if(i==t) { str += "<option selected='selected'>"+i+"</option>"; } else { str += "<option>"+i+"</option>"; } } document.getElementById("ri").innerHTML = str; } function Bian() { //天 Tian(); } </script> </html>
这是这个例题的最终效果,他可以选择一个年限内的年份,而且月份可以随着年份的变化而变换,日也是。