zoukankan      html  css  js  c++  java
  • 9月23日JavaScript作业----日期时间选择

    作业二:日期时间选择

    <div style="600px; height:100px;"> 
      <select id="year"></select>年
      <select id="month" onchange="FillDay()"></select>月
      <select id="day"></select>日 
    </div>
    </body>
    <script type="text/javascript">
    FillYear();
    FillMonth();
    FillDay();
    function FillYear()
    {
      var sj = new Date();//现在的日期时间
      var nian = sj.getFullYear();//获取年份 
      var s = "";
      for(var i=nian-5;i<nian+6;i++)//上下都是5年,i里面存的是年
      {
        if(i==nian)//如果i等于当前的年,也就是2016年。
        {
          s +="<option selected='selected'>"+i+"</option>";//下拉列表中默认出现的年份
        }
        else
        {
          s +="<option>"+i+"</option>";//普通的年份
        }
      }
    
      document.getElementById("year").innerHTML = s;//把这个字符串给年份的下拉
    }
    function FillMonth()
    {
      var sj = new Date();//在这个位置调用
      var yue = sj.getMonth()+1;    
      var s = "";
      for(var i=1;i<13;i++)
      {
        if(i==yue)
        {
          s +="<option selected='selected'>"+i+"</option>";
        }
        else
        {
          s +="<option>"+i+"</option>";
        }
      }    
      document.getElementById("month").innerHTML=s;
    }
    function FillDay()
    {
      var sj = new Date();
      var tian = sj.getDate();    
      var yue = document.getElementById("month").value;  取月份求天数
      var n = 31;
      if(yue==4 || yue==6 ||yue==9 ||yue==11)
      {
        n = 30;
      }
      else if(yue==2)
      {
        n=28;
      }    
      var s = "";  用循环添加
      for(var i=1;i<n+1;i++)
      {
        if(i==tian)
        {
          s +="<option selected='selected'>"+i+"</option>";
        }
        else
        {
          s +="<option>"+i+"</option>";
        }
      }    
      document.getElementById("day").innerHTML = s;    
    }
  • 相关阅读:
    Python与数据库
    初识matplotlib
    Jquery--实现轮播图
    Juery入门2
    CSS布局方式
    Jquery入门一
    html-DOM了解
    jquery --入门
    JS练习
    kettle 报错汇总
  • 原文地址:https://www.cnblogs.com/xiaofox0018/p/5899418.html
Copyright © 2011-2022 走看看