鑒於網上找到的幾個日期選擇程序有些問題
開始本想使用window
不過做到中途發現用createPopup窗口實現理論上是行不通的
一是因為不在窗體裡單擊任何地方都會關閉窗口
二是窗體的寬度和高度只能在彈出時設置
鑒於上面的原因
Calendar
/**
*本日歷選擇控件由tiannet根據前人經驗完善而得
*tiannet添加了時間選擇功能
*使用方法
* (
* (
* (
*設置參數的方法
* (
* (
* (
* (
* (
*說明
* 默認返回的日期時間格式如同
*/
//
//功能按鈕同樣樣式
var s_tiannet_turn_base = "height:
//翻年
var s_tiannet_turn = "width:
//關閉
var s_tiannet_turn
//年選擇下拉框
var s_tiannet_select = "width:
//月
var s_tiannet_select
//日期選擇控件體的樣式
var s_tiannet_body = "width:
"border
//顯示日的td的樣式
var s_tiannet_day = "width:
//字體樣式
var s_tiannet_font = "color:#FFCC
//鏈接的樣式
var s_tiannet_link = "text
//橫線
var s_tiannet_line = "border
//
var tiannetYearSt =
var tiannetYearEnd =
var tiannetDateNow = new Date();
var tiannetYear = tiannetDateNow
var tiannetMonth = tiannetDateNow
var tiannetDay = tiannetDateNow
var tiannetHour =
var tiannetMinute =
var tiannetArrDay=new Array(
var tiannetDateSplit = "
var tiannetDateTimeSplit = " "; //日期與時間之間的分隔符
var tiannetTimeSplit = ":"; //時間的分隔符號
var tiannetOutObject; //接收日期時間的對象
var arrTiannetHide = new Array();//被強制隱藏的標簽
var m_bolShowHour = false;//是否顯示小時
var m_bolShowMinute = false;//是否顯示分鐘
var m_aMonHead = new Array(
m_aMonHead[
m_aMonHead[
//
//用戶主調函數-只選擇日期
function setDay(obj){
tiannetOutObject = obj;
//如果標簽中有值
var strValue = tiannetTrim(tiannetOutObject
if( strValue != "" ){
tiannetInitDate(strValue);
}
tiannetPopCalendar();
}
//用戶主調函數-選擇日期和小時
function setDayH(obj){
tiannetOutObject = obj;
m_bolShowHour = true;
//如果標簽中有值
var strValue = tiannetTrim(tiannetOutObject
if( strValue != "" ){
tiannetInitDate(strValue
var hour = strValue
if( hour <
}
tiannetPopCalendar();
}
//用戶主調函數-選擇日期和小時及分鐘
function setDayHM(obj){
tiannetOutObject = obj;
m_bolShowHour = true;
m_bolShowMinute = true;
//如果標簽中有值
var strValue = tiannetTrim(tiannetOutObject
if( strValue != "" ){
tiannetInitDate(strValue
var time = strValue
var arr = time
tiannetHour = arr[
tiannetMinute = arr[
if( tiannetHour <
if( tiannetMinute <
}
tiannetPopCalendar();
}
//設置開始日期和結束日期
function setYearPeriod(intDateBeg
tiannetYearSt = intDateBeg;
tiannetYearEnd = intDateEnd;
}
//設置日期分隔符
function setDateSplit(strDateSplit){
tiannetDateSplit = strDateSplit;
}
//設置日期與時間之間的分隔符
function setDateTimeSplit(strDateTimeSplit){
tiannetDateTimeSplit = strDateTimeSplit;
}
//設置時間分隔符
function setTimeSplit(strTimeSplit){
tiannetTimeSplit = strTimeSplit;
}
//設置分隔符
function setSplit(strDateSplit
tiannetDateSplit(strDateSplit);
tiannetDateTimeSplit(strDateTimeSplit);
tiannetTimeSplit(strTimeSplit);
}
//設置默認的日期
function setDefaultDate(strDate){
tiannetYear = strDate
tiannetMonth = strDate
tiannetDay = strDate
}
//設置默認的時間
function setDefaultTime(strTime){
tiannetHour = strTime
tiannetMinute = strTime
}
//
//
var weekName = new Array("日"
document
document
document
document
for(var i=tiannetYearSt;i <= tiannetYearEnd;i ++){
document
}
document
document
document
for(var i=
document
}
document
//document
//document
document
document
for(var i=
document
}
document
document
document
for(var i=
document
}
document
document
//輸出一條橫線
document
document
document
document
document
document
document
//輸出一條橫線
document
document
document
for(var i =
//輸出星期
document
}
document
document
//輸出天的選擇
document
var n =
for (var i=
document
for (var j=
document
+
n ++;
}
document
}
document
document
+
document
+
document
document
document
document
//
//
//單擊年份span標簽響應
function spanYearCEvent(){
hideElementsById(new Array("selTianYear"
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead")
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead")
hideElementsById(new Array("tiannetYearHead"
}
//單擊月份span標簽響應
function spanMonthCEvent(){
hideElementsById(new Array("selTianMonth"
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead")
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead")
hideElementsById(new Array("tiannetMonthHead"
}
//單擊小時span標簽響應
function spanHourCEvent(){
hideElementsById(new Array("tiannetYearHead"
if(m_bolShowHour) hideElementsById(new Array("selTianHour")
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead")
hideElementsById(new Array("tiannetHourHead"
}
//單擊分鐘span標簽響應
function spanMinuteCEvent(){
hideElementsById(new Array("tiannetYearHead"
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead")
if(m_bolShowMinute) hideElementsById(new Array("selTianMinute")
hideElementsById(new Array("tiannetMinuteHead"
}
//根據標簽id隱藏或顯示標簽
function hideElementsById(arrId
var strDisplay = "";
if(bolHide) strDisplay = "none";
for(var i =
var obj = document
obj
}
}
//
//判斷某年是否為閏年
function isPinYear(year){
var bolRet = false;
if (
bolRet = true;
}
return bolRet;
}
//得到一個月的天數
function getMonthCount(year
var c=m_aMonHead[month
if((month==
return c;
}
//重新設置當前的日
function setRealDayCount() {
if( tiannetDay > getMonthCount(tiannetYear
//如果當前的日大於當月的最大日
tiannetDay = getMonthCount(tiannetYear
}
}
//在個位數前加零
function addZero(value){
if(value <
value = "
}
return value;
}
//取出空格
function tiannetTrim(str) {
return str
}
//為select創建一個option
function createOption(objSelect
var option = document
option
option
objSelect
}
//往前翻 Year
function tiannetPrevYear() {
if(tiannetYear >
else{alert("年份超出范圍(
tiannetSetDay(tiannetYear
//如果年份小於允許的最小年份
if( tiannetYear < tiannetYearSt ) {
tiannetYearSt = tiannetYear;
createOption(document
}
checkSelect(document
tiannetWriteHead();
}
//往後翻 Year
function tiannetNextYear() {
if(tiannetYear >
else{alert("年份超出范圍(
tiannetSetDay(tiannetYear
//如果年份超過允許的最大年份
if( tiannetYear > tiannetYearEnd ) {
tiannetYearEnd = tiannetYear;
createOption(document
}
checkSelect(document
tiannetWriteHead();
}
//選擇今天
function tiannetToday() {
tiannetYear = tiannetDateNow
tiannetMonth = tiannetDateNow
tiannetDay = tiannetDateNow
tiannetSetValue(true);
//tiannetSetDay(tiannetYear
//selectObject();
}
//往前翻月份
function tiannetPrevMonth() {
if(tiannetMonth>
tiannetSetDay(tiannetYear
checkSelect(document
tiannetWriteHead();
}
//往後翻月份
function tiannetNextMonth() {
if(tiannetMonth==
tiannetSetDay(tiannetYear
checkSelect(document
tiannetWriteHead();
}
//向span標簽中寫入年
function tiannetWriteHead(){
document
document
if( m_bolShowHour ) document
if( m_bolShowMinute ) document
tiannetSetValue(false);//給文本框賦值
}
//設置顯示天
function tiannetSetDay(yy
setRealDayCount();//設置當月真實的日
tiannetWriteHead();
var strDateFont
for (var i =
var day
var firstday = new Date(yy
for (var i = firstday; day
tiannetArrDay[i]=day
}
//如果用於顯示日的最後一行的第一個單元格的值為空
//if(tiannetArrDay[
// document
//} else {
// document
//}
for (var i =
var da = eval("document
if (tiannetArrDay[i]!="") {
//判斷是否為周末
if(i %
strDateFont
strDateFont
} else {
strDateFont
strDateFont
}
da
//如果是當前選擇的天
if(tiannetArrDay[i] == tiannetDay ) {
da
} else {
da
}
da
} else {
da
}
}//end for
tiannetSetValue(false);//給文本框賦值
}//end function tiannetSetDay
//根據option的值選中option
function checkSelect(objSelect
var count = parseInt(objSelect
if( selectValue <
selectValue = selectValue
}
for(var i =
if(objSelect
objSelect
break;
}
}//for
}
//選中年
function selectObject(){
//如果年份小於允許的最小年份
if( tiannetYear < tiannetYearSt ) {
for( var i = tiannetYear;i < tiannetYearSt;i ++ ){
createOption(document
}
tiannetYearSt = tiannetYear;
}
//如果年份超過允許的最大年份
if( tiannetYear > tiannetYearEnd ) {
for( var i = tiannetYearEnd+
createOption(document
}
tiannetYearEnd = tiannetYear;
}
checkSelect(document
checkSelect(document
if( m_bolShowHour ) checkSelect(document
if( m_bolShowMinute ) checkSelect(document
}
//給接受日期時間的控件賦值
//參數bolHideControl
function tiannetSetValue(bolHideControl){
var value = "";
if( !tiannetDay || tiannetDay == "" ){
tiannetOutObject
return;
}
var mm = tiannetMonth;
var day = tiannetDay;
if( mm <
if( day <
value = tiannetYear + tiannetDateSplit + mm + tiannetDateSplit + day;
if( m_bolShowHour ){
var hour = tiannetHour;
if( hour <
value += tiannetDateTimeSplit + hour;
}
if( m_bolShowMinute ){
var minute = tiannetMinute;
if( minute <
value += tiannetTimeSplit + minute;
}
tiannetOutObject
//document
if( bolHideControl ) {
tiannetHideControl();
}
}
//是否顯示時間
function showTime(){
if( !m_bolShowHour && m_bolShowMinute){
alert("如果要選擇分鐘
return;
}
hideElementsById(new Array("tiannetHourHead"
if( m_bolShowHour ){
//顯示小時
hideElementsById(new Array("tiannetHourHead")
}
if( m_bolShowMinute ){
//顯示分鐘
hideElementsById(new Array("tiannetMinuteHead")
}
}
//彈出顯示日歷選擇控件
function tiannetPopCalendar(){
//隱藏下拉框
hideElementsById(new Array("selTianYear"
hideElementsById(new Array("tiannetYearHead"
tiannetSetDay(tiannetYear
tiannetWriteHead();
showTime();
var dads = document
var iX
var h = document
var w = document
//計算left
if (window
iX = window
else
iX = window
if (iX <
iX=
//計算top
iY = window
if (window
iY = document
else
iY = document
if (iY <
iY=
dads
dads
tiannetShowControl();
selectObject();
}
//隱藏日歷控件(同時顯示被強制隱藏的標簽)
function tiannetHideControl(){
document
tiannetShowObject();
arrTiannetHide = new Array();//將被隱藏的標簽對象清空
}
//顯示日歷控件(同時隱藏會遮擋的標簽)
function tiannetShowControl(){
document
tiannetHideObject("SELECT");
tiannetHideObject("OBJECT");
}
//根據標簽名稱隱藏標簽
function tiannetHideObject(strTagName) {
x = document
y = document
h = document
w = document
for (var i =
{
var obj = document
if (! obj || ! obj
continue;
// 獲取元素對於BODY標記的相對坐標
var objLeft = obj
var objTop = obj
var objHeight = obj
var objWidth = obj
var objParent = obj
while (objParent
objLeft += objParent
objTop += objParent
objParent = objParent
}
//alert("控件左端:" + x + "select左端" + (objLeft + objWidth) + "控件底部:" + (y+h) + "select高:" + objTop);
var bolHide = true;
if( obj
//如果標簽本身就是隱藏的
bolHide = false;
}
if( ( (objLeft + objWidth) > x && (y + h +
//arrTiannetHide
arrTiannetHide[arrTiannetHide
obj
}
}
}
//顯示被隱藏的標簽
function tiannetShowObject(){
for(var i =
//alert(arrTiannetHide[i]);
arrTiannetHide[i]
}
}
//初始化日期
function tiannetInitDate(strDate){
var arr = strDate
tiannetYear = arr[
tiannetMonth = arr[
tiannetDay = arr[
}
//清空
function tiannetClear(){
tiannetOutObject
tiannetHideControl();
}
//任意點擊時關閉該控件
function document
with(window
if (tagName != "INPUT" && getAttribute("Author") != "tiannet")
tiannetHideControl();
}
}
//按ESC鍵關閉該控件
function document
if( event
tiannetHideControl();
}
}
Calendar
<!DOCTYPE HTML PUBLIC "
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio
<meta name="ProgId" content="VisualStudio
<meta name="Originator" content="Microsoft Visual Studio
<script src="
</head>
<body>
(
(
(
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20261.html