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;    
    }
  • 相关阅读:
    2款JS脚本判断手机浏览器跳转WAP手机网站
    js实现域名判断后跳转到指定网址
    js实现网页多少秒后自动跳转到指定网址
    利用JS判断当前来路域名并跳转到指定页面
    网站建设中用JS判断时间并显示不同内容
    python 基础_列表的其他操作 4
    Codeforces Round #519 by Botan Investments
    HDU5794
    牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph
    2018年牛客多校算法寒假训练营练习比赛(第一场)C. 六子冲
  • 原文地址:https://www.cnblogs.com/xiaofox0018/p/5899418.html
Copyright © 2011-2022 走看看