时间操作(JavaScript版)—年月日三级联动(默认显示系统时间)
版权声明:本文为博主原创文章,未经博主同意不得转载。
https://blog.csdn.net/wangshuxuncom/article/details/35263317
这个功能是大学时自己使用纯JavaScript写的,没有借助Jquery,呵呵呵,看起来有点繁琐,但是在当时依稀的记得功能实现后自己好好的高兴一把了呢,从如今来看那时候的自己是多么的幼稚、多么的无知:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>年月日三级联动(默认显示系统时间)</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript">
function removeChilds(id){
var childs = document.getElementById(id).childNodes;//这一行代码和紧跟的以下的for循环用于清除原来日的下拉列表的select中的对节点
for(var i=childs.length-1;i>=0;i--) {
document.getElementById(id).removeChild(childs[i]);
}
}
function setDay(){
var yearToDate=document.getElementById("year").value;
var monthToDate=document.getElementById("month").value;
//alert(yearToDate+":"+monthToDate);
var days=new Array(28,29,30,31);
var nowDate=new Date();
if(monthToDate==1||monthToDate==3||monthToDate==5||monthToDate==7||monthToDate==8||monthToDate==10||monthToDate==12){
removeChilds("day");
for( i=1; i<=days[3]; i++ ){
if(yearToDate==nowDate.getFullYear()&&monthToDate==(nowDate.getMonth()+1)&&i==nowDate.getDate()){//假设是当前系统时间则设置默认的日
var newOption = document.createElement("option");newOption.setAttribute("value",i);newOption.setAttribute("selected","selected");
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}else{
var newOption = document.createElement("option");newOption.setAttribute("value",i);
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}
}
}
if(monthToDate==4||monthToDate==6||monthToDate==9||monthToDate==11){
removeChilds("day");
for( i=1; i<=days[2]; i++ ){
if(yearToDate==nowDate.getFullYear()&&monthToDate==(nowDate.getMonth()+1)&&i==nowDate.getDate()){//假设是当前系统时间则设置默认的日
var newOption = document.createElement("option");newOption.setAttribute("value",i);newOption.setAttribute("selected","selected");
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}else{
var newOption = document.createElement("option");newOption.setAttribute("value",i);
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}
}
}
if(monthToDate==2){
removeChilds("day");
if(yearToDate%400==0||yearToDate%100!=0&&yearToDate%4==0){//闰年
for( i=1; i<=days[1]; i++ ){
if(yearToDate==nowDate.getFullYear()&&monthToDate==(nowDate.getMonth()+1)&&i==nowDate.getDate()){//假设是当前系统时间则设置默认的日
var newOption = document.createElement("option");newOption.setAttribute("value",i);newOption.setAttribute("selected","selected");
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}else{
var newOption = document.createElement("option");newOption.setAttribute("value",i);
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}
}
}
else{
for( i=1; i<=days[0]; i++ ){
if(yearToDate==nowDate.getFullYear()&&monthToDate==(nowDate.getMonth()+1)&&i==nowDate.getDate()){//假设是当前系统时间则设置默认的日
var newOption = document.createElement("option");newOption.setAttribute("value",i);newOption.setAttribute("selected","selected");
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}else{
var newOption = document.createElement("option");newOption.setAttribute("value",i);
var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);
document.getElementById("day").appendChild(newOption);
}
}
}
}
}
function getMonth(){
var m;
for(m=1;m<=12;m++) {
if((new Date().getMonth()+1)==m){
document.write("<option value="+m+" selected=\"selected\">"+m+"</option>");
}else{
document.write("<option value="+m+">"+m+"</option>");
}
}
}
function getYear(){
var y;
var date=new Date();
var fullYear=date.getFullYear();
for(y=fullYear-60;y<=fullYear;y++){
if(y==fullYear){
document.write("<option value="+y+" selected=\"selected\">"+y+"</option>");
}else{
document.write("<option value="+y+" >"+y+"</option>");
}
}
}
</script>
</head>
<body>
<select name="year" id="year" onChange="setDay();"><script type="text/javascript">getYear();</script></select>年
<select name="month" id="month" onChange="setDay()"><script type="text/javascript">getMonth();</script></select>月
<select name="day" id="day"><script type="text/javascript">setDay();<!--起到初始化日的作用。
--></script></select>日
</body>
</html>
时间操作(JavaScript版)—年月日三级联动(默认显示系统时间)的更多相关文章
- JS实现年月日三级联动+省市区三级联动+国家省市三级联动
开篇随笔:最近项目需要用到关于年月日三级联动以及省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例或者只有单纯的js还不完整,却很难找到详细的具体数据(baidu搜索都 ...
- 利用select实现年月日三级联动的日期选择效果
× 目录 [1]演示 [2]规划 [3]结构生成[4]算法处理 前面的话 关于select控件,可能年月日三级联动的日期选择效果是最常见的应用了.本文是选择框脚本的实践,下面将对日期选择效果进行详细介 ...
- JS 实现的年月日三级联动
js文件 SYT="-请选择年份-"; SMT="-请选择月份-"; SDT="-请选择日期-"; BYN=50;//年份范围往前50年 A ...
- JS年月日三级联动下拉框日期选择代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- LaTeX去掉默认显示日期时间
LaTeX去掉默认显示日期时间: \date{}
- Qt 实时显示系统时间
前言 我们用一个label控件来实时显示系统时间,用到 QTimer 和 QDateTime 这个两个类. 正题 头文件: #ifndef MAINWINDOW_H #define MAINWINDO ...
- js实现年月日三级联动
当我们注册一个qq的时候,会看到一个三级年月日的联动菜单,下面简单介绍. <!doctype html> <html lang="en"> <head ...
- 原生javascript制作省市区三级联动详细教程
多级联动下拉菜单是前端常见的效果,省市区三级联动又属于其中最典型的案例.多级联动一般都是与数据相关联的,根据数据来生成和修改联动的下拉菜单.完成一个多级联动效果,有助于增强对数据处理的能力. 本实例以 ...
- 原生javascript实现省市区三级联动
腾讯IP分享计划(http://ip.qq.com/)有个现成的三级联动功能,查看源码后发现可以直接使用其单独的JS文件(http://ip.qq.com/js/geo.js). 分析后发现自己需要写 ...
随机推荐
- python-day21--序列化模块模块
什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化 序列化的目的: 1.以某种存储形式使自定义对象持久化: 2.将对象从一个地方传递到另一个地方. 3.使程序更具维护性. ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- canvas绘制直线
HTML5 <canvas>元素用于图形的绘制,该元素只是图形容器,必须使用javascript脚本绘制图形创建一个画布:<canvas id="myCanvas" ...
- swiper中的默认值的属性和作用(小程序交流群:604788754)
swiper中的重要属性: vertical:属性,控制swiper效果是水平切换滚动,还是垂直切换滚动.如果不设置此属性,默认是水平滚动,如果设置:vertical="true" ...
- SQL Server 调优系列进阶篇 - 查询语句运行几个指标值监测
前言 上一篇我们分析了查询优化器的工作方式,其中包括:查询优化器的详细运行步骤.筛选条件分析.索引项优化等信息. 本篇我们分析在我们运行的过程中几个关键指标值的检测. 通过这些指标值来分析语句的运行问 ...
- (C/C++学习笔记) 十六. 预处理
十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...
- MyEclipse 2017 Stable 2.0发布|附下载
MyEclipse个人开发者专享6.9折!仅剩最后3天!在线订购>> 2017 Stable 2.0版本带来了新的功能和修复,从Stable 1.0以来,已经发布了几个版本的Angular ...
- DevExpress XtraScheduler日程管理控件应用实例(2)-- 深入理解数据存储
DevExpress年终击穿底价,单套授权低至67折!查看详情>>> 在上篇随笔<DevExpress XtraScheduler日程管理控件应用实例(1)-- 基本使用> ...
- slf4j简单使用
一 slf4j+log4j 1.添加依赖 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <d ...
- 使用jenkins进行项目的自动构建部署
jenkins 简介 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括:持续的软件版本发布/测试项目和监控外部调用执行的工作. 官网地址地址: https://je ...