日历问题,我用jsp还是javascript哪个好用,如何实现,请指教!!!

ludy 2002-03-27 01:46:08
我现在有三个下拉列表框,一个是年,一个是月,一个是日,我选择2002年,在选择2月,日的自动显示28天,也就是,日那个下拉列表框是自动判断他的月份是大月还是小月还是闰月等。请详细给出代码,多谢了!!!
...全文
11 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weidegong 2002-03-27
daysGrid += '<b>';
if (isThisMonth) { daysGrid += '<font color=red>' + months[month] + '</font>'; }
else { daysGrid += months[month]; }
daysGrid += '</b>';

daysGrid += '<a href="javascript:incMonth(1,\'' + eltName + '\')"> »</a>';
daysGrid += '   ';
daysGrid += '<a href="javascript:incYear(-1,\'' + eltName + '\')">« </a>';

daysGrid += '<b>';
if (isThisYear) { daysGrid += '<font color=red>' + year + '</font>'; }
else { daysGrid += ''+year; }
daysGrid += '</b>';

daysGrid += '<a href="javascript:incYear(1,\'' + eltName + '\')"> »</a><br>';
daysGrid += ' Su Mo Tu We Th Fr Sa <br> ';
var dayOfMonthOfFirstSunday = (7 - startDay + 1);
for (var intWeek = 0; intWeek < 6; intWeek++) {
var dayOfMonth;
for (var intDay = 0; intDay < 7; intDay++) {
dayOfMonth = (intWeek * 7) + intDay + dayOfMonthOfFirstSunday - 7;
if (dayOfMonth <= 0) {
daysGrid += "   ";
} else if (dayOfMonth <= intDaysInMonth) {
var color = "blue";
if (day > 0 && day == dayOfMonth) color="red";
daysGrid += '<a href="javascript:setDay(';
daysGrid += dayOfMonth + ',\'' + eltName + '\')" '
daysGrid += 'style="color:' + color + '">';
var dayString = dayOfMonth + "</a> ";
if (dayString.length == 6) dayString = '0' + dayString;
daysGrid += dayString;
}
}
if (dayOfMonth < intDaysInMonth) daysGrid += "<br> ";
}
return daysGrid + "</td></tr></table>";
}

function setDay(day,eltName) {
displayElement.value = (displayMonth + 1) + "/" + day + "/" + displayYear;
hideElement(eltName);
}


//--------------------------------------

<!--
// fixPosition() 这个函数和前面所讲的那个函数一样
//
function fixPosition(eltname) {
elt = getXBrowserRef(eltname);
positionerImgName = eltname + 'Pos';
// hint: try setting isPlacedUnder to false
isPlacedUnder = false;
if (isPlacedUnder) {
setPosition(elt,positionerImgName,true);
} else {
setPosition(elt,positionerImgName)
}
}

function toggleDatePicker(eltName,formElt) {
var x = formElt.indexOf('.');
var formName = formElt.substring(0,x);
var formEltName = formElt.substring(x+1);
newCalendar(eltName,document.forms[formName].elements[formEltName]);
toggleVisible(eltName);
}

// fixPositions() 这个函数前面也讲过
function fixPositions()
{
fixPosition('daysOfMonth');
fixPosition('daysOfMonth2');
}

// -->
</SCRIPT>
<META content="MSHTML 5.50.4807.2300" name=GENERATOR>
</HEAD>
<BODY onresize=fixPositions() bgColor=#ffffff>
<P> 
<FORM name=date>
<CENTER>
<TABLE width=225 border=1>
<TBODY>
<TR>
<TD width=89>开始日期: </TD>
<TD width=120>
<INPUT size=10 name=ret>
<IMG
onmouseup="toggleDatePicker('daysOfMonth','date.ret')" id=daysOfMonthPos
height=19 alt="date picker"
src="tr1010-xiaoguo2.files/tr1010-calendaricon.gif" width=19
align=absMiddle border=0 name=daysOfMonthPos>
<DIV id=daysOfMonth style="POSITION: absolute"></DIV>
</TD>
</TR>
<TR>
<TD width=89>终止日期: </TD>
<TD width=120>
<INPUT size=10 name=ret2>
<IMG
onmouseup="toggleDatePicker('daysOfMonth2','date.ret2')"
id=daysOfMonth2Pos height=19 alt="date picker"
src="tr1010-xiaoguo2.files/tr1010-calendaricon.gif" width=19
align=absMiddle border=0 name=daysOfMonth2Pos>
<DIV id=daysOfMonth2
style="POSITION: absolute"></DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
</FORM>
<P>
<SCRIPT language=JavaScript1.2>

function Cancel() {
hideElement("daysOfMonth");
}

</SCRIPT>
<SCRIPT language=JavaScript1.2>
<!--
hideElement('daysOfMonth');
hideElement('daysOfMonth2');
//-->
</SCRIPT>
</P>
</BODY>
</HTML>

---
觉得这个更好用的话,多给点分吧
回复
weidegong 2002-03-27
<HEAD>
<TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JavaScript1.2>

// 一个简单的测试是否IE浏览器的表达式
isIE = (document.all ? true : false);

// 得到IE中各元素真正的位移量,即使这个元素在一个表格中
function getIEPosX(elt) { return getIEPos(elt,"Left"); }
function getIEPosY(elt) { return getIEPos(elt,"Top"); }
function getIEPos(elt,which) {
iPos = 0
while (elt!=null) {
iPos += elt["offset" + which]
elt = elt.offsetParent
}
return iPos
}

function getXBrowserRef(eltname) {
return (isIE ? document.all[eltname].style : document.layers[eltname]);
}

function hideElement(eltname) { getXBrowserRef(eltname).visibility = 'hidden'; }

// 按不同的浏览器进行处理元件的位置
function moveBy(elt,deltaX,deltaY) {
if (isIE) {
elt.left = elt.pixelLeft + deltaX;
elt.top = elt.pixelTop + deltaY;
} else {
elt.left += deltaX;
elt.top += deltaY;
}
}

function toggleVisible(eltname) {
elt = getXBrowserRef(eltname);
if (elt.visibility == 'visible' || elt.visibility == 'show') {
elt.visibility = 'hidden';
} else {
fixPosition(eltname);
elt.visibility = 'visible';
}
}

function setPosition(elt,positionername,isPlacedUnder) {
positioner = null;
if (isIE) {
positioner = document.all[positionername];
elt.left = getIEPosX(positioner);
elt.top = getIEPosY(positioner);
} else {
positioner = document.images[positionername];
elt.left = positioner.x;
elt.top = positioner.y;
}
if (isPlacedUnder) { moveBy(elt,0,positioner.height); }
}



//--------------------------------------

// 判断浏览器
isIE = (document.all ? true : false);

// 初始月份及各月份天数数组
var months = new Array("一 月", "二 月", "三 月", "四 月", "五 月", "六 月", "七 月",
"八 月", "九 月", "十 月", "十一月", "十二月");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31);
var displayMonth = new Date().getMonth();
var displayYear = new Date().getFullYear();
var displayDivName;
var displayElement;

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(eltName,attachedElement) {
if (attachedElement) {
if (displayDivName && displayDivName != eltName) hideElement(displayDivName);
displayElement = attachedElement;
}
displayDivName = eltName;
today = new getToday();
var parseYear = parseInt(displayYear + '');
var newCal = new Date(parseYear,displayMonth,1);
var day = -1;
var startDayOfWeek = newCal.getDay();
if ((today.year == newCal.getFullYear()) &&
(today.month == newCal.getMonth()))
{
day = today.day;
}
var intDaysInMonth =
getDays(newCal.getMonth(), newCal.getFullYear());
var daysGrid = makeDaysGrid(startDayOfWeek,day,intDaysInMonth,newCal,eltName)
if (isIE) {
var elt = document.all[eltName];
elt.innerHTML = daysGrid;
} else {
var elt = document.layers[eltName].document;
elt.open();
elt.write(daysGrid);
elt.close();
}
}

function incMonth(delta,eltName) {
displayMonth += delta;
if (displayMonth >= 12) {
displayMonth = 0;
incYear(1,eltName);
} else if (displayMonth <= -1) {
displayMonth = 11;
incYear(-1,eltName);
} else {
newCalendar(eltName);
}
}

function incYear(delta,eltName) {
displayYear = parseInt(displayYear + '') + delta;
newCalendar(eltName);
}

function makeDaysGrid(startDay,day,intDaysInMonth,newCal,eltName) {
var daysGrid;
var month = newCal.getMonth();
var year = newCal.getFullYear();
var isThisYear = (year == new Date().getFullYear());
var isThisMonth = (day > -1)
daysGrid = '<table border=1 cellspacing=0 cellpadding=2><tr><td bgcolor=#ffffff nowrap>';
daysGrid += '<font face="courier new, courier" size=2>';
daysGrid += '<a href="javascript:hideElement(\'' + eltName + '\')">x</a>';
daysGrid += '  ';
daysGrid += '<a href="javascript:incMonth(-1,\'' + eltName + '\')">« </a>';

回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告