JavaScript特效实例013-日期选择器

实例013                              日期选择器

实例说明

弹出日期选择器来供用户来选择日期。

技术要点

本例主要使用window对象的showModalDialog()方法实现。

实现过程

(1)用来弹出日期选择器的主页面。点击图片弹出日期选择器,然后在文本框输出选择的日期。

<!DOCTYPE html>
<html>
<head>
	<title>日期选择器</title>
	<meta charset="utf-8"/>
	<script type="text/javascript">
	function pp(field)
	{
		var rtn = window.showModalDialog("time.html","","dialogWidth:280px;dialogHeight:250px;status:no;help:no;scrolling=no;scrollbars=no");
		if(rtn!=null)
		{
			field.value=rtn;
			return;
		}
	}
	</script>
</head>
<body>
<form id="form1" name="form1">
	<input type="text" name="time1" id="time1"/>
	<img src="Images/new.jpg" width="20px" height="20px" onclick="pp(form1.time1)">
</form>
</body>
</html>

(2)日期选择器界面time.html

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8"/>
	<script type="text/javascript">
	var monthNames = new Array("","1","2","3","4","5","6","7","8","9","10","11","12");
	var endDay = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	var dayNow = 0;
	var monthNow = 0;
	var yearNow = 0;
	function load(form)
	{
		set_month_year_now();
		var found = false;
		for(var month=0;month<form.monthList.length;month++)
		{
			if(form.monthList[month].text == monthNames[monthNow])
			{
				form.monthList[month].selected = true;
				found = true;
			}
		}
		if(!found)
		{
			error();
			return;
		}
		display_month(form);
	}
	function preceding_month(form)
	{
		var month_selected = form.monthList.selectedIndex;
		var year_selected = form.yearList.selectedIndex;
		if(!month_selected&&!year_selected)
		{
			error();
			return;
		}
		if(month_selected>0)
		{
			month_selected--;
		}
		else
		{
			month_selected=11;
			year_selected--;
		}
		form.monthList[month_selected].selected = true;
		form.yearList[year_selected].selected = true;
		display_month(form);
	}
	function following_month(form)
	{
		var month_selected = form.monthList.selectedIndex;
		var year_selected = form.yearList.selectedIndex;
		if(month_selected>=(form.monthList.length-1)&&year_selected>=(form.yearList.length-1))
		{
			error();
			return;
		}
		if(month_selected<11)
		{
			month_selected++;
		}
		else{
			month_selected=0;
			year_selected++;
		}
		form.monthList[month_selected].selected = true;
		form.yearList[year_selected].selected = true;
		display_month(form);
	}
	function set_month_year_now()
	{
		var form = document.calendar;
		var now = new Date();
		monthNow = now.getMonth()+1;
		yearNow = now.getYear();
		dayNow = now.getDate();
		yearNow = yearNow+1900;
		var count = 0;
		for(var i = yearNow-103;i<yearNow+50;i++)
		{
			eval("form.yearList.options[count] = new Option('"+i+"','"+i+"')");
			count++;
		}
		form.yearList.selectedIndex = 103;
		form.yearList.length = count;
	}
	function error()
	{
		alert("超出范围!");
	}
	function display_month(form)
	{
		var month = form.monthList.selectedIndex+1;
		var year = parseInt(form.yearList.options[form.yearList.selectedIndex].text);
		var start_day = start_day_in_month(year,month);
		var count = 0;
		for(var row = 0;row<6;row++)
		{
			for(var col = 0;col<7;col++)
			{
				if(row == 0&&col<(start_day-1))
					var day = "";
				else if(count<endDay[month])
					day = ++count;
				else
					day = "";
				form.dayBox[(row*7)+col].style.display = "";
				form.dayBox[(row*7)+col].style.color = "black";
				if(day=="")
				{
					form.dayBox[(row*7)+col].style.display = "none";
				}
				else{
					form.dayBox[(row*7)+col].value = day;
					if(col%7==0)
					{
						form.dayBox[(row*7)+col].style.color = "red";
					}
					if(yearNow == year&&monthNow == month&&dayNow ==day)
						form.dayBox[(row*7)+col].style.color = "blue";
				}
			}
		}
	}
	function start_day_in_month(year,month)
	{
		var sday,daynum,ndays,mnum;
		sday = start_day_in_year(year);
		endDay[2] = (year%4)?28:29;
		if(month == 1)
		{
			daynum = sday;
		}
		else{
			ndays = sday;
			for(mnum = 2;mnum<month+1;mnum++)
			{
				ndays = ndays+endDay[mnum-1];
				daynum = ndays%7;
			}
			daynum = (!daynum)?7:daynum;
			return(daynum);
		}
	}
	function start_day_in_year(year)
	{
		var y,m,d;
		var n;
		y = year-1;
		m = 13;
		d = 1;
		n = d+2*m+(Math.floor((0.6+(m+1)))+y);
		n = n+Math.floor(((y/4)-Math.floor((y/100))+Math.floor((y/400))))+2;
		n = Math.floor(((n/7-Math.floor((n/7)))*7+0.5));
		return (n+1);
	}
	function CheckDate(strDay)
	{
		var docFrm = document.calendar;
		var choice_daynum = 0;
		var current_daynum = 0;
		var day_temp;
		if(strDay!="")
		{
			var strY = docFrm.yearList.value;
			var strM = docFrm.monthList.value;
			var curr_y = new String(yearNow);
			var curr_m = new String(monthNow);
			var curr_d = new String(dayNow);
			if(curr_m.length == 1)
				curr_m = "0"+curr_m;
			if(curr_d.length == 1)
				curr_d = "0"+curr_d;
			current_daynum = new Number(curr_y+curr_m+curr_d);
			if(strM.length == 1)
				strM = "0"+strM;
			if(strDay.length == 1)
				strDay = "0"+strDay;
			choice_daynum = new Number(strY+strM+strDay);
			parent.window.returnValue = strY+"-"+strM+"-"+strDay;
			parent.window.close();
		}
		return false;
	}
	</script>
</head>
<body onLoad="load(document.calendar)" topmargin="0">
<center>
	<form name="calendar">
		<table border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td colspan="3" height="24"></td>
			</tr>
			<tr>
				<td width="205" align="right" valign="middle" nowrap="nowrap">
					<select name="yearList" onchange="display_month(this.form)">

					</select>
				</td>
				<td width="65" nowrap="nowrap" align="left">
					<select name="monthList" size="1" onchange="display_month(this.form)">
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7</option>
						<option value="8">8</option>
						<option value="9">9</option>
						<option value="10">10</option>
						<option value="11">11</option>
						<option value="12">12</option>
					</select>
				</td>
				<td width="10"></td>
			</tr>
			<tr>
				<td colspan="3" height="6"></td>
			</tr>
			<tr>
				<td colspan="3">
					<table border="0" cellpadding="1" cellspacing="0" align="center">
						<tr>
							<td bgcolor="#82664F">
								<table border="0" cellspacing="0" cellpadding="0">
									<tr bgcolor="#82664F" height="18">
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											<font color="#FF0000">日</font>
										</td>
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											一
										</td>
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											二
										</td>
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											三
										</td>
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											四
										</td>
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											五
										</td>
										<td width="31" align="center" nowrap="nowrap" bgcolor="#0099FF">
											六
										</td>
									</tr>
									<tr bgcolor="#FFFFFF" height="18">
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
									</tr>
									<tr bgcolor="#FFFFFF" height="18">
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
									</tr>
									<tr bgcolor="#FFFFFF" height="18">
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
									</tr>
									<tr bgcolor="#FFFFFF" height="18">
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
									</tr>
									<tr bgcolor="#FFFFFF" height="18">
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
									</tr>
									<tr bgcolor="#FFFFFF" height="18">
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
										<td align="center">
											<input type="text" size="2" name="dayBox" readOnly onclick="javascript:CheckDate(this.value);" onMouseOver="this.style.background='#EEEEEE'" onmouseout="this.style.background='white'"/>
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>
</center>
</body>
</html>

这样本例就做好了。

时间: 2024-10-10 20:33:24

JavaScript特效实例013-日期选择器的相关文章

10种JavaScript特效实例让你的网站更吸引人

我们有三种主要的方法(从难到易):自己动手写脚本;使用类似于jQuery和mooTools的JavaScript框架(可以让编写代码变得更容易些);使用能工作于现有的JavaScript框架下的提前预置好的脚本或那种从头开始开发的创建者.这篇文章适合那些打算提高工作效率或不愿意从头开发脚本的人阅读. 您还可以参考以下JavaScript/Ajax相关教程及资源:<10个非常棒的Ajax及Javascript实例资源网站><12种Javascript解决常见浏览器兼容问题的方法>&l

JavaScript特效实例018-下降式浏览器

实例018                               下降式浏览器 实例说明 本实例是在窗口打开时,将整个窗口放在屏幕的最上面,使窗口无法在屏幕中进行显示,然后动态地使窗口进行下移,直到窗口显示在屏幕的左上角为止. 技术要点 本实例主要应用了screen对象的availHesht属性来获得当前屏幕工作区的高度,并用window对象的moveBy()方法使窗口自动下移.下面对window对象的moveBy()方法进行详细的介绍. moveBy()方法的语法格式: window.m

JavaScript特效实例001-打开新窗口显示广告信息

实例001                 打开新窗口显示广告信息 实例说明 本实例要在窗口每次被加载的时候弹出一个广告对话框. 技术要点 本实例主要应用JavaScript的window对象. window对象的常用方法 方法 说明 alert() 弹出一个警告对话框 confirm() 在确认对话框中显示指定的字符串 prompt() 弹出一个提示对话框 close() 关闭被引用的窗口 focus() 将被引用的窗口放在所有打开窗口的前面 open() 打开新浏览器窗口并且显示由URL或名

JavaScript特效实例004-自动关闭的广告窗口

实例004                  自动关闭的广告窗口 实例说明 当网页运行一定的时间后,自动关闭网页上指定的广告窗口. 技术要点 本实例主要应用window对象的setTimeout()方法和close()方法来实习.window对象的setTimeout()方法用于延迟执行某一操作. 实现过程 (1)要关闭的广告窗口. new.html <html> <head> <title>打开新窗口显示广告信息</title> <style typ

JavaScript特效实例003-通过按钮创建窗口

实例003                     通过按钮创建窗口 实例说明 点击按钮创建窗口 技术要点 主要是使用window是对象的open()方法来创建和调用其他窗口. 实现过程 1.制作用于点击按钮打开的窗口广告页面 <pre name="code" class="html"><html> <head> <title>打开新窗口显示广告信息</title> <style type="

JavaScript特效实例002-定时打开窗口

实例002                                  定时打开窗口 实例说明 本实例要在指定的时间后打开窗口. 技术要点 主要应用window对象setTimeout()方法实现定时打开窗口. setTime()方法的语法格式如下. <span style="font-size:14px;">setTimeout(function,milliseconds)</span> 参数说明如下. function:要调用的JavaScript自定

JavaScript特效实例011-弹出全屏显示的网页模式对话框

实例011                    弹出全屏显示的网页模式对话框 实例说明 弹出全屏显示的网页模式对话框,用户关闭之前不能浏览网站的其他内容. 技术要点 本实例主要应用screen对象的width.height属性和window对象的showModalDialog()方法实现.其实还有一种方法打开网页对话框,即showModelessDialog()方法. 使用showModalDialog()与showModelessDialog()方法的区别在于,showModalDialog

JavaScript特效实例014-页面自动滚动

实例014                                页面自动滚动 实例说明 本实例实现在打开页面,当页面出现纵向滚动条时,页面中的内容将从上向下进行滚动. 技术要点 本例主要是使用window对象的scroll()方法指定窗口的当前位置.下面对scroll()方法进行详细说明. scroll()方法的语法格式: scroll(x,y); 参数说明如下. 1.x:屏幕的横向坐标 2.y屏幕的纵向坐标 功能:指定窗口滚动坐标的位置. 实现过程 用于实现功能的主页面index.h

JavaScript特效实例005-控制弹出窗口居中显示

实例005                控制弹出窗口居中显示 实例说明 点击页面上的按钮,弹出窗口,并将窗口居中显示. 技术要点 本例首先应用JavaScript中的window对象的open()方法,打开指定大小的新窗口,然后通过screen对象,获取屏幕分辨率,再根据获取的值通过window对象的moveTo()方法,将新窗口移动到屏幕居中位置. moveTo()方法将窗口移动到指定坐标(x,y)处,其语法格式如下. window.moveTo(x,y) 其中,参数x,y表示窗口移动到的位