`
fudehai001
  • 浏览: 489632 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

javascript做日期选择器实例

阅读更多

                       
  1.新建selectDateAll.js
  var months = new Array("一月", "二月", "三月","四月", "五月", "六月", "七月", "八月", "九月","十月", "十一月", "十二月");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var getObject,oldObject;//取得当前对象
//取得指定年月的天数
function getDays(month, year) {
   if (1 == month)
      return ((0==year%4)&&(0!=(year%100)))||(0==year%400)?29:28;
   else
      return daysInMonth[month];
}
//取得今天的年,月,日
function getToday() {
   this.now = new Date();
   this.year = this.now.getFullYear();
   this.month = this.now.getMonth();
   this.day = this.now.getDate();
}
today = new getToday();
//创建日历
function newCalendar() {
   today = new getToday();
   var parseYear = parseInt(document.all.year[document.all.year.selectedIndex].text);
   var newCal = new Date(parseYear,document.all.month.selectedIndex, 1);
   var day = -1;
   var startDay = newCal.getDay();
   var daily = 0;
   if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))day = today.day;
   var tableCal = document.all.calendar.tBodies.dayList;
   var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
   for (var intWeek = 0; intWeek < tableCal.rows.length;intWeek++)
       for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++){
         var cell = tableCal.rows[intWeek].cells[intDay];
  if ((intDay == startDay) && (0 == daily))daily = 1;
  if(day==daily)cell.className = "today";//今天的Class
  else if(intDay==6)cell.className = "sunday";//周六的Class
  else if (intDay==0)cell.className ="satday";//周日的Class
  else cell.className="normal";//平常的Class
  if ((daily > 0) && (daily <= intDaysInMonth)){
   cell.innerText = daily;
   daily++;
  }else cell.innerText = "";
   }
}
//取得鼠标单击处的日期数据
function getDate() {
   var sDate;
   if ("TD" == event.srcElement.tagName)
      if ("" != event.srcElement.innerText){//定义显示日期的格式
sMonth = document.all.month.value;
sDay = event.srcElement.innerText;
if(sMonth.length==1)sMonth = "0"+sMonth;
if(sDay.length==1)sDay = "0"+sDay;
sDate = document.all.year.value + "-" + sMonth + "-" + sDay;
getObject.value=sDate;
window.close();
  }
}
//隐藏图层
function HideLayer() {
Layer.style.visibility = "hidden";
}
//显示图层
function LayerShow(){
Layer.style.visibility = "visible";
}
//显示div对象Layer
function ShowLayer(t,l) {
if(oldObject==null)oldObject=getObject;//赋值
if(Layer.style.visibility != "visible" && oldObject==getObject){//判断是否操作同一表
Layer.style.top = t+document.body.scrollTop;
Layer.style.left = l+document.body.scrollLeft;
Layer.style.visibility = "visible";
oldObject=getObject;
}else if(oldObject==getObject){
HideLayer();
}else{
Layer.style.top = t+document.body.scrollTop;
Layer.style.left = l+document.body.scrollLeft;
Layer.style.visibility = "visible";
oldObject=getObject;
}
}
//定义div对象Layer用以显示日期
function getLayer(){
document.write("<div id=\"Layer\" style=\"position:absolute;width:340;z-index:12;boder-color:#ffffff;border:0px outset white;background-color:#D4D0C8;layer-background-color:#D4D0C8;visibility:hidden;height:38;left:58;top:137;\"><input type=\"hidden\" name=\"ret\"><table width='320' id=\"calendar\" cellpadding=\"0\" align=\"center\"><thead><tr><td height='4'></td></tr><tr><td colspan=7 align=CENTER>请选择月份:<select id=\"month\" onChange=\"newCalendar()\" name=\"select\" class=\"smallSel\">");
for (var intLoop = 0; intLoop < months.length;  intLoop++)
document.write("<OPTION VALUE= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop]);
document.write("</select> &nbsp;&nbsp;&nbsp;&nbsp;请选择年份:<select id=\"year\" onChange=\"newCalendar()\" name=\"select\" class=\"smallSel\">");
for (var intLoop = today.year-50; intLoop < (today.year + 10); intLoop++)
document.write("<OPTION VALUE= "+intLoop+" "+(today.year == intLoop ?  "Selected" : "") + ">" + intLoop);
document.write("</select></td></tr><tr><td height='6'></td></tr><tr class=\"days\">");
document.write("<TD class=sunday>" + days[0] + "</TD>");
for (var intLoop = 1; intLoop < days.length-1;intLoop++)
document.write("<TD>" + days[intLoop] + "</TD>");
document.write("<TD class=sunday>" + days[intLoop] + "</TD>");
document.write(" </tr></thead><tbody border=1 cellspacing=\"0\" cellpadding=\"0\" id=\"dayList\" align=CENTER ONCLICK='getDate()'>");
for (var intWeeks = 0; intWeeks < 6; intWeeks++){
document.write("<TR style='cursor:hand'>");
for (var intDays = 0; intDays < days.length;intDays++)document.write("<TD></TD>");
document.write("</TR>");
}
document.write("</tbody></table></div>");
}
//定义css
document.write("<style>TABLE{font- family:宋体,SimSun; font-size:9pt;border:0px;bgcolor:#D4D0C8;}.drpdwn {font-family:宋体,SimSun;font-size:9pt;color:#000000;background- color:#000000} SELECT.smallSel{    BACKGROUND-COLOR: #eeeeee;    COLOR: #000000;    FONT-SIZE: 9pt} .normal{BACKGROUND: #D4D0C8;} .today {font-weight:bold;background-image:  url(date.gif);} .satday{color:#800000} .sunday{color:#800000} .days {FONT-SIZE: 9pt;} .Arraw {color:#0000BB; cursor:hand; font-family:Webdings; font-size:9pt}</style>");

getLayer();//显示日期选择器

//选择日期
function selectDate(x){
getObject=x;
newCalendar();
ShowLayer(0,0);//显示出div
}

2 新建selectDate.htm
  <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>请选择日期</title>
</head>
<script language="JavaScript" src="selectDateAll.js"></script>
<script language="JavaScript">
function returns(){
window.returnValue = dates.value;
}
</script>
<body bgcolor="#d4d0c8"
  leftmargin="0"
  rightmargin="0"
  bottommargin="0"
  topmargin="0"
  onLoad="selectDate(dates)"
  onUnload="returns()">
<input type="hidden" name="dates" id="dates">
</body>
</html>

3 建立index.htm
  <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>日期选择示例</title>
</head>
<script language="JavaScript">
<!--
function popup(test){
  var xx=event.clientX;
  var yy=event.clientY;
  test.value=window.showModalDialog("selectDate.htm","","dialogWidth:350px;dialogHeight:180px;dialogLeft:"+xx+"px;dialogTop:"+yy+"px;status:0;");
}
-->
</script>
<body>
请输入日期:
<input name="test" id="test" type="text" onClick="popup(this)">
</body>
</html>

分享到:
评论

相关推荐

    vue日期选择器控件

    vue日期选择器控件

    JavaScript应用实例-文件选择器.js

    JavaScript应用实例-文件选择器.js

    javascript网页特效实例大全(13-19)

    实例360 日期选择器 580 实例361 弹出提示对话框并重定向网页 584 实例362 打开指定大小的新窗口并居中显示 586 15.2 弹出确认对话框 588 实例363 删除数据前弹出确认对话框 588 实例364 安全退出前弹出...

    JavaScript应用实例-ui下对话框文件选择器(1).js

    JavaScript应用实例-ui下对话框文件选择器(1).js

    JavaScript实例精通

    18_4.htm jQuery的选择器之变态演示。 18_5.htm 让链接打开一个新窗口。 18_6.htm 键盘控制选择表格并编辑。 18_7.htm 下拉框的值同时给文本框和文本域。 18_8.htm 基于jQuery的折叠菜单。 18_9.htm 可...

    程序天下:JavaScript实例自学手册

    13.36 全面的日期选择功能 13.37 全球的时间查看表 13.38 无刷新定时取数据 13.39 取当月的最后一天 第 14章 数字、数组和统计函数特效 14.1 边打字边显示字数 14.2 创建随机数 14.3 JavaScript创建二维数组 14.4 ...

    JavaScript经典实例

     2.4找到并突出显示一个模式的所有实例  2.5使用新字符串替换模式  2.6使用捕获圆括号交换一个字符串中的单词  2.7使用正则表达式来去除空白  2.8使用命名实体来替代HTML标签  2.9搜索特殊字符  第3章日期、...

    JavaScript网页特效范例宝典源码

    实例013 日期选择器 21 1.3 窗口的动画效果 26 实例014 页面自动滚动 26 实例015 打开窗口特殊效果 27 实例016 动态显示窗口 29 实例017 慢慢放大的窗口 30 实例018 下降式浏览器 32 实例019 旋转的窗口 33 实例020 ...

    《JavaScript实例精通》[源代码]

    18_4.htm jQuery的选择器之变态演示。 18_5.htm 让链接打开一个新窗口。 18_6.htm 键盘控制选择表格并编辑。 18_7.htm 下拉框的值同时给文本框和文本域。 18_8.htm 基于jQuery的折叠菜单。 18_9.htm 可...

    源文件程序天下JAVASCRIPT实例自学手册

    1.3.2 选择JavaScript脚本编辑器 1.4 如何引入JavaScript脚本代码 1.4.1 通过[removed]与[removed]标记对引入 1.4.2 通过[removed]标记的src属性引入 1.4.3 通过JavaScript伪URL引入 1.4.4 通过HTML文档事件处理程序...

    《程序天下:JavaScript实例自学手册》光盘源码

    13.36 全面的日期选择功能 13.37 全球的时间查看表 13.38 无刷新定时取数据 13.39 取当月的最后一天 第 14章 数字、数组和统计函数特效 14.1 边打字边显示字数 14.2 创建随机数 14.3 JavaScript创建二维数组 14.4 ...

    js实现精确到秒的日期选择器完整实例

    主要介绍了js实现精确到秒的日期选择器,以完整实例形式分析了JavaScript日期选择器的实现步骤与相关技巧,涉及JavaScript针对时间与日期的相关运算,需要的朋友可以参考下

    【JavaScript源代码】vue 图标选择器的实例代码.docx

    vue 图标选择器的实例代码  来源:http://www.ruoyi.vip/  import Vue from 'vue'  import SvgIcon from '@/components/SvgIcon'// svg component   // register globally Vue.component('svg-icon', ...

    JavaScript基础和实例代码

    1.3.2 选择JavaScript脚本编辑器 1.4 如何引入JavaScript脚本代码 1.4.1 通过与标记对引入 1.4.2 通过标记的src属性引入 1.4.3 通过JavaScript伪URL引入 1.4.4 通过HTML文档事件处理程序引入 1.5 嵌入JavaScript脚本...

    【JavaScript源代码】elementUI实现级联选择器.docx

     本文实例为大家分享了elementUI实现级联选择器的具体代码,供大家参考,具体内容如下 1、从后端调用接口,传递数据到前端 2、使用VUE代码显示级联选项  :disabled="isDisabled" :props="defaultParams" :...

    【JavaScript源代码】基于Vant UI框架实现时间段选择器.docx

     本文实例为大家分享了Vant UI框架实现时间段选择器的具体代码,供大家参考,具体内容如下 组件代码如下: &lt;template&gt;  :title="title" :show-toolbar="showToolbar" :columns="columns" @confirm=...

    javascript网页特效实例大全

    3.16 图片选择器 50 3.17 链接导航框 51 3.18 小球跟踪链接 52 3.19 单击按钮打开全屏窗口 56 3.20 单选按钮选择链接 56 3.21 隐藏的链接 57 3.22 变换链接颜色 58 3.23 固定链接的位置 59 第4章 时间类特效...

    javascript网页特效实例大全(8-12)

    实例265 颜色拾取器 411 9.3 广告页面 415 实例266 图片总置于顶端 415 实例267 随机显示广告 416 实例268 广告随滚动条漂移 418 9.4 页面动画效果 419 实例269 下雪 419 实例270 飘落的枫叶 421 ...

    纯JavaScript网页取色器代码.rar

    纯JS实现的网页取色器代码,或者叫拾色器,颜色选择器选色使用技巧:1.双击颜色 或 单击颜色,微调亮度,点确定2.色彩亮度微调:在右边竖立的颜色条中进行,下载本程序方法:在IE菜单中,点 文件 按钮,点保存即可。或...

Global site tag (gtag.js) - Google Analytics