jsp中的时间选择器的代码(转载)

以下这个代码是我在开发中用的作为一个时间选择器的空间,向数据库中插入日期的做法。 

 

<script language="JavaScript">
var gdCtrl = new Object();
var gcGray = "#808080";
var gcToggle = "highlight";
var gcBG = "threedface";
var gMonths = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
var gdCurDate = new Date();
var giYear = gdCurDate.getFullYear();
var giMonth = gdCurDate.getMonth()+1;
var giDay = gdCurDate.getDate();
var sxYear = giYear;
var sxMonth = giMonth;
var sxDay = giDay;
var sxDatestr = gdCtrl.value;
if (sxDatestr != ""){
var sxDate = new Date(sxDatestr);
sxYear = sxDate.getFullYear();
}
var VicPopCal = new Object();
function mouseover(obj){
obj.style.borderTop = 'buttonshadow 1px solid';
obj.style.borderLeft = 'buttonshadow 1px solid';
obj.style.borderRight = 'buttonhighlight 1px solid';
obj.style.borderBottom = 'buttonhighlight 1px solid';
}
function mouseout(obj){
obj.style.borderTop = 'buttonhighlight 1px solid';
obj.style.borderLeft = 'buttonhighlight 1px solid';
obj.style.borderRight = 'buttonshadow 1px solid';
obj.style.borderBottom = 'buttonshadow 1px solid';
}
function mousedown(obj){
obj.style.borderTop = 'buttonshadow 1px solid';
obj.style.borderLeft = 'buttonshadow 1px solid';
obj.style.borderRight = 'buttonhighlight 1px solid';
obj.style.borderBottom = 'buttonhighlight 1px solid';
}
function mouseup(obj){
obj.style.borderTop = 'buttonhighlight 1px solid';
obj.style.borderLeft = 'buttonhighlight 1px solid';
obj.style.borderRight = 'buttonshadow 1px solid';
obj.style.borderBottom = 'buttonshadow 1px solid';
}
function fPopCalendar(popCtrl, dateCtrl, popCal){
parent.event.cancelBubble=true;
VicPopCal = popCal;
gdCtrl = dateCtrl;
fSetYearMon(giYear, giMonth);
var point = fGetXY(popCtrl);
with (VicPopCal.style) {left = point.x;top  = point.y+popCtrl.offsetHeight+1;visibility = 'visible';}
VicPopCal.focus();
}
function fSetDate(iYear, iMonth, iDay){
if ((iYear == 0) && (iMonth == 0) && (iDay == 0)){
gdCtrl.value = "";
}
else{
if (iMonth < 10){iMonth = "0"+iMonth;}
if (iDay < 10){iDay = "0"+iDay;}
gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
}
VicPopCal.style.visibility = "hidden";
}
function fSetSelected(aCell){
var iOffset = 0;
var iYear = parseInt(document.all.tbSelYear.value);
var iMonth = parseInt(document.all.tbSelMonth.value);
aCell.bgColor = gcBG;
with (aCell.children["cellText"]){
var iDay = parseInt(innerText);
if (color==gcGray){iOffset = (Victor<10)?-1:1;}
iMonth += iOffset;
if (iMonth<1) { iYear--; iMonth = 12;}else{if (iMonth>12){iYear++;iMonth = 1;}}
}
fSetDate(iYear, iMonth, iDay);
}
function Point(iX, iY){this.x = iX;this.y = iY;}
function fBuildCal(iYear, iMonth){
var aMonth=new Array();
for(i=1;i<7;i++){aMonth[i]=new Array(i);}
var dCalDate=new Date(iYear, iMonth-1, 1);
var iDayOfFirst=dCalDate.getDay();
var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();
var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;
var iDate = 1;
var iNext = 1;
for (d = 0; d < 7; d++){aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;}
for (w = 2; w < 7; w++){for (d = 0; d < 7; d++){aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);}}
return aMonth;
}
function fDrawCal(iYear, iMonth, iDay, iCellWidth, iDateTextSize) {
var WeekDay = new Array("日","一","二","三","四","五","六");
var styleTD = " bgcolor='"+gcBG+"' width='"+iCellWidth+"' bordercolor='"+gcBG+"' valign='middle' align='center' style='font-size: 12px;background: buttonface;border-top: buttonhighlight 1px solid;border-left: buttonhighlight 1px solid;border-right: buttonshadow 1px solid; border-bottom: buttonshadow 1px solid;";
with (document) {
write("<tr align='center'>");
for(i=0; i<7; i++){write("<td height='20' "+styleTD+"color:#990099' >" + WeekDay[i] + "</td>");}

write("</tr>");
for (w = 1; w < 7; w++) {
write("<tr align='center'>");
for (d = 0; d < 7; d++) {
write("<td width='10%' height='15' id=calCell "+styleTD+"cursor:hand;' οnmοuseοver='mouseover(this)' οnmοuseοut='mouseout(this)' οnmοusedοwn='mousedown(this)' οnmοuseup='mouseup(this)' οnclick='fSetSelected(this)'>");
write("<font style='font-size: 13px;' id=cellText Victor='Liming Weng'> </font>");
write("</td>");
}
write("</tr>");
}
}
}
function fUpdateCal(iYear, iMonth) {
sxYear = iYear;
sxMonth = iMonth;
yeartd1.innerText = sxYear + "年";
monthtd1.innerText = gMonths[sxMonth-1];
myMonth = fBuildCal(iYear, iMonth);
var i = 0;
for (w = 0; w < 6; w++){
for (d = 0; d < 7; d++){
with (cellText[(7*w)+d]) {
Victor = i++;
if (myMonth[w+1][d]<0) {
color = gcGray;
innerText = -myMonth[w+1][d];
}else{
color = ((d==0)||(d==6))?"red":"black";
innerText = myMonth[w+1][d];
}
}
}
}
}
function fSetYearMon(iYear, iMon){
sxYear = iYear;
sxMonth = iMon;
yeartd1.innerText = sxYear + "年";
monthtd1.innerText = gMonths[sxMonth-1];
document.all.tbSelMonth.options[iMon-1].selected = true;
for (i = 0; i < document.all.tbSelYear.length; i++){
if (document.all.tbSelYear.options[i].value == iYear){
document.all.tbSelYear.options[i].selected = true;
}
}
fUpdateCal(iYear, iMon);
}
function fPrevMonth(){
var iMon = document.all.tbSelMonth.value;
var iYear = document.all.tbSelYear.value;
if (--iMon<1) {
iMon = 12;
iYear--;
}
fSetYearMon(iYear, iMon);
}
function fNextMonth(){
var iMon = document.all.tbSelMonth.value;
var iYear = document.all.tbSelYear.value;
if (++iMon>12) {
iMon = 1;
iYear++;
}
fSetYearMon(iYear, iMon);
}
function fGetXY(aTag){
var oTmp = aTag;
var pt = new Point(0,0);
do {
pt.x += oTmp.offsetLeft;
pt.y += oTmp.offsetTop;
oTmp = oTmp.offsetParent;
} while(oTmp.tagName!="BODY");
return pt;
}
with (document){
write("<Div id='PopCal' οnclick='event.cancelBubble=true' style='POSITION:absolute; VISIBILITY: hidden; bordercolor:#000000;border:2px ridge;width:10;z-index:100;'>");
write("<table id='popTable' border='1' bgcolor='#eeede8' cellpadding='0' cellspacing='0' style='font-size:12px'>");
write("<TR>");
write("<td valign='middle' align='center' style='cursor:default'>");
write("<table width='176' border='0' cellpadding='0' cellspacing='0'>");
write("<tr align='center'>");
write("<td height='22' width='20' name='PrevMonth' style='font-family:/"webdings/";font-size:15px' onClick='fPrevMonth()' οnmοuseοver='this.style.color=/"#ff9900/"' οnmοuseοut='this.style.color=/"/"'>3</td>");
write("<td width='64' id='yeartd1' style='font-size:12px' οnmοuseοver='yeartd1.style.display=/"none/";yeartd2.style.display=/"/";' οnmοuseοut='this.style.background=/"/"'>");
write(sxYear + "年");
write("</td>");
write("<td width='64' id='yeartd2' style='display:none' οnmοuseοut='yeartd2.style.display=/"none/";yeartd1.style.display=/"/";'>");
write("<SELECT style='width:64px;font-size: 12px;font-family: 宋体;' id='tbSelYear' onChange='fUpdateCal(document.all.tbSelYear.value, document.all.tbSelMonth.value);yeartd2.style.display=/"none/";yeartd1.style.display=/"/";' Victor='Won'>");
for(i=1930;i<2015;i++){
write("<OPTION value='"+i+"'>"+i+"年</OPTION>");
}
write("</SELECT>");
write("</td>");
write("<td width='72' id='monthtd1' style='font-size:12px' οnmοuseοver='monthtd1.style.display=/"none/";monthtd2.style.display=/"/";' οnmοuseοut='this.style.background=/"/"'>");
write(gMonths[sxMonth-1]);
write("</td>");
write("<td width='72' id='monthtd2' style='display:none' οnmοuseοut='monthtd2.style.display=/"none/";monthtd1.style.display=/"/";'>");
write("<select style='width:72px;font-size: 12px;font-family: 宋体;' id='tbSelMonth' onChange='fUpdateCal(document.all.tbSelYear.value, document.all.tbSelMonth.value);monthtd2.style.display=/"none/";monthtd1.style.display=/"/";' Victor='Won'>");
for (i=0; i<12; i++){
write("<option value='"+(i+1)+"'>"+gMonths[i]+"</option>");
}
write("</SELECT>");
write("</td>");
write("<td width='20' name='PrevMonth' style='font-family:/"webdings/";font-size:15px' οnclick='fNextMonth()' οnmοuseοver='this.style.color=/"#ff9900/"' οnmοuseοut='this.style.color=/"/"'>4</td>");
write("</tr>");
write("</table>");
write("</td></TR><TR><td align='center'>");
write("<DIV style='background-color:teal;'><table width='100%' border='0' bgcolor='threedface' cellpadding='0' cellspacing='0'>");
fDrawCal(giYear, giMonth, giDay, 19, 14);
write("</table></DIV>");
write("</td></TR><TR><TD height='20' align='center' valign='bottom'>");
write("<font style='cursor:hand;font-size:12px' οnclick='fSetDate(0,0,0)' onMouseOver='this.style.color=/"#0033FF/"' onMouseOut='this.style.color=0'>清空</font>");
write("&nbsp;&nbsp;&nbsp;&nbsp;");
write("<font style='cursor:hand;font-size:12px' οnclick='fSetDate(giYear,giMonth,giDay)' onMouseOver='this.style.color=/"#0033FF/"' onMouseOut='this.style.color=0'>今天: "+giYear+"-"+giMonth+"-"+giDay+"</font>");
write("</TD></TR></TD></TR></TABLE>");
write("</Div>");
}
</script>

 

以上是js部分,下面是在jsp中如何的调用(就是如何条用js,这个和我们平常使用的方法一样,比较简单)....

<input alt="点击可以选择日期" name="news_time" type="text" style="cursor:hand" onFocus="fPopCalendar(this,this,PopCal); return false;" onClick="javascript:this.focus()" value="<%=news_time%>" readonly="readonly"  />

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值