网站建设资讯

NEWS

网站建设资讯

用javascript编写日历,js简单日历代码

如何用JavaScript编一个简单的日历

script language=javascript

在永昌等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、成都做网站 网站设计制作定制网站开发,公司网站建设,企业网站建设,品牌网站建设,网络营销推广,成都外贸网站建设,永昌网站建设费用合理。

var DS_x,DS_y;

function dateSelector() //构造dateSelector对象,用来实现一个日历形式的日期输入框。

{

var myDate=new Date();

this.year=myDate.getFullYear(); //定义year属性,年份,默认值为当前系统年份。

this.month=myDate.getMonth()+1; //定义month属性,月份,默认值为当前系统月份。

this.date=myDate.getDate(); //定义date属性,日,默认值为当前系统的日。

this.inputName=''; //定义inputName属性,即输入框的name,默认值为空。注意:在同一页中出现多个日期输入框,不能有重复的name!

this.display=display; //定义display方法,用来显示日期输入框。

}

function display() //定义dateSelector的display方法,它将实现一个日历形式的日期选择框。

{

var week=new Array('日','一','二','三','四','五','六');

document.write("style type=text/css");

document.write(" .ds_font td,span { font: normal 12px 宋体; color: #000000; }");

document.write(" .ds_border { border: 1px solid #000000; cursor: hand; background-color: #DDDDDD }");

document.write(" .ds_border2 { border: 1px solid #000000; cursor: hand; background-color: #DDDDDD }");

document.write("/style");

document.write("input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly");

document.write("button style='width:60px;height:18px;font-size:12px;margin:1px;border:1px solid #A4B3C8;background-color:#DFE7EF;' type=button onclick=this.nextSibling.style.display='block' onfocus=this.blur()选择日期/button");

document.write("div style='position:absolute;display:none;text-align:center;width:0px;height:0px;overflow:visible' onselectstart='return false;'");

document.write(" div style='position:absolute;left:-60px;top:20px;width:142px;height:165px;background-color:#F6F6F6;border:1px solid #245B7D;' class=ds_font");

document.write(" table cellpadding=0 cellspacing=1 width=140 height=20 bgcolor=#CEDAE7 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'");

document.write(" tr align=center");

document.write(" td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subYear(this) title='减小年份'/td");

document.write(" td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subMonth(this) title='减小月份'/td");

document.write(" td width=52%b"+this.year+"/bb年/bb"+this.month+"/bb月/b/td");

document.write(" td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addMonth(this) title='增加月份'/td");

document.write(" td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addYear(this) title='增加年份'/td");

document.write(" /tr");

document.write(" /table");

document.write(" table cellpadding=0 cellspacing=0 width=140 height=20 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'");

document.write(" tr align=center");

for(i=0;i7;i++)

document.write(" td"+week[i]+"/td");

document.write(" /tr");

document.write(" /table");

document.write(" table cellpadding=0 cellspacing=2 width=140 bgcolor=#EEEEEE");

for(i=0;i6;i++)

{

document.write(" tr align=center");

for(j=0;j7;j++)

document.write(" td width=10% height=16 onmouseover=if(this.innerText!=''this.className!='ds_border2')this.className='ds_border' onmouseout=if(this.className!='ds_border2')this.className='' onclick=getvalue(this,document.all('DS_"+this.inputName+"'))/td");

document.write(" /tr");

}

document.write(" /table");

document.write(" span style=cursor:hand onclick=this.parentNode.parentNode.style.display='none'【关闭】/span");

document.write(" /div");

document.write("/div");

dateShow(document.all("DS_"+this.inputName).nextSibling.nextSibling.childNodes[0].childNodes[2],this.year,this.month)

}

function subYear(obj) //减小年份

{

var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

myObj[0].innerHTML=eval(myObj[0].innerHTML)-1;

dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

}

function addYear(obj) //增加年份

{

var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

myObj[0].innerHTML=eval(myObj[0].innerHTML)+1;

dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

}

function subMonth(obj) //减小月份

{

var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

var month=eval(myObj[2].innerHTML)-1;

if(month==0)

{

month=12;

subYear(obj);

}

myObj[2].innerHTML=month;

dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

}

function addMonth(obj) //增加月份

{

var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes;

var month=eval(myObj[2].innerHTML)+1;

if(month==13)

{

month=1;

addYear(obj);

}

myObj[2].innerHTML=month;

dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML))

}

function dateShow(obj,year,month) //显示各月份的日

{

var myDate=new Date(year,month-1,1);

var today=new Date();

var day=myDate.getDay();

var selectDate=obj.parentNode.parentNode.previousSibling.previousSibling.value.split('-');

var length;

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

length=31;

break;

case 4:

case 6:

case 9:

case 11:

length=30;

break;

case 2:

if((year%4==0)(year%100!=0)||(year%400==0))

length=29;

else

length=28;

}

for(i=0;iobj.cells.length;i++)

{

obj.cells[i].innerHTML='';

obj.cells[i].style.color='';

obj.cells[i].className='';

}

for(i=0;ilength;i++)

{

obj.cells[i+day].innerHTML=(i+1);

if(year==today.getFullYear()(month-1)==today.getMonth()(i+1)==today.getDate())

obj.cells[i+day].style.color='red';

if(year==eval(selectDate[0])month==eval(selectDate[1])(i+1)==eval(selectDate[2]))

obj.cells[i+day].className='ds_border2';

}

}

function getvalue(obj,inputObj) //把选择的日期传给输入框

{

var myObj=inputObj.nextSibling.nextSibling.childNodes[0].childNodes[0].cells[2].childNodes;

if(obj.innerHTML)

inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-"+obj.innerHTML;

inputObj.nextSibling.nextSibling.style.display='none';

for(i=0;iobj.parentNode.parentNode.parentNode.cells.length;i++)

obj.parentNode.parentNode.parentNode.cells[i].className='';

obj.className='ds_border2'

}

function dsMove(obj) //实现层的拖移

{

if(event.button==1)

{

var X=obj.clientLeft;

var Y=obj.clientTop;

obj.style.pixelLeft=X+(event.x-DS_x);

obj.style.pixelTop=Y+(event.y-DS_y);

}

}

/script

script language=javascript

var myDate=new dateSelector();

myDate.year--;

myDate.inputName='start_date'; //注意这里设置输入框的name,同一页中日期输入框,不能出现重复的name。

myDate.display();

/script

javascript html实现网页版日历代码

本文实例为大家分享了网页版日历代码,供大家参考,具体内容如下

效果图:

实现代码:

html

head

link

rel="stylesheet"

type="text/css"

href="Skin.css"

style

!--

table{

text-align:

center

}

--

/style

/head

body

div

align="center"

script

language="javascript"

var

my

=

new

Date();

function

showc()

{

var

k=1;

var

j=1;

var

today;

var

tomonth;

var

theday=1;//日期

var

max;

var

temp;

var

tempday;//这个月第一天的星期

document.write

("b"

+

my.getFullYear()

+

"-"

+

(my.getMonth()+1)

+

"/b");

document.write

("table

border='1'

width='273'

height='158'");

document.write

("tr");

document.write

("td

height='23'

width='39'font

color='red'Sun/font/td");

document.write

("td

height='23'

width='39'Mon/td");

document.write

("td

height='23'

width='39'Tue/td");

document.write

("td

height='23'

width='39'Wed/td");

document.write

("td

height='23'

width='39'Thu/td");

document.write

("td

height='23'

width='39'Fri/td");

document.write

("td

height='23'

width='39'Sat/td");

document.write

("/tr");

temp=my.getDate();

my.setDate(1);

//document.write

(my.getDate());

tempday=my.getDay();//返回第一天是星期几

my.setDate(temp);

today=my.getDay();//返回现在星期几

switch

((my.getMonth()+1))

{

case

1:

case

3:

case

5:

case

7:

case

8:

case

10:

case

12:

max=31;

break;

case

4:

case

6:

case

9:

case

11:

max=30;

break;

default:

max=29;//这里没有考虑闰月!!

//document.write

(max);

}

for(k=0;k6;k++)

{

document.write

("tr");

for(j=0;j=6;j++)

{

document.write

("td

height='23'

width='39'");

if(j=(tempday))

{

tempday=0;//设置为最小,相当于取消判断条件

if(theday=max)

{

document.write

("a

title="

+

my.getFullYear()

+

"-"

+

(my.getMonth()+1)

+

"-"

+theday

+

"

target='_blank'

href=detail.asp?date="

+

theday

+

"");

if(theday==my.getDate())

document.write

("font

color='green'["

+

theday

+

"]/font/a");

else

if(j==0)

document.write

("font

color='red'"

+

theday

+

"/font/a");

else

document.write

(theday

+

"/a");

theday++;

}

}

document.write

("/td");

}

document.write

("/tr");

}

document.write

("/table");

}

showc();

/script

/div

body

/html

以上就是本文的全部内容,希望大家可以轻松实现网页版日历。

PC端如何用js做点击日历选中一周

1.写一个获取当前时间的脚本

2.写一个获取事件源绝对坐标的脚本

3.写一个日历生成脚本,根据年(闰年)月(大小月)周(周一到周日)生成日历

4.当事件源(一般是文本框)获得焦点的时候,触发获取绝对坐标的事件,然后生成当前月的日历,将此日历的坐标用脚本移动到文本框下方

5.生成日历的时候,给所有日添加返回事件,把当前选中的年月日反馈

关于用JAVASCRIPT做日历的问题!

html

headmeta http-equiv="Content-Type" content="text/html; charset=gb2312"

title日期选择 - BY ziyue/title

script language="JavaScript" type="text/JavaScript"

/*=======Calendar.js=======By Jiang Hongbin=======*/

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 today;

document.writeln("div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'/div");

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();

}

function getStringDay(str)

{

//得到输入框的年,月,日

var str=str.split("-")

this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));

this.year = this.now.getFullYear();

this.month = this.now.getMonth();

this.day = this.now.getDate();

}

function newCalendar() {

var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);

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;

var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());

for (var intWeek = 1; 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) //今天,调用今天的Class

{

cell.style.background='#6699CC';

cell.style.color='#FFFFFF';

//cell.style.fontWeight='bold';

}

else if(intDay==6) //周六

cell.style.color='green';

else if (intDay==0) //周日

cell.style.color='red';

if ((daily 0) (daily = intDaysInMonth))

{

cell.innerText = daily;

daily++;

}

else

cell.innerText = "";

}

}

function GetDate(InputBox)

{

var sDate;

//这段代码处理鼠标点击的情况

if (event.srcElement.tagName == "TD")

if (event.srcElement.innerText != "")

{

sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;

eval("document.all."+InputBox).value=sDate;

HiddenCalendar();

}

}

function HiddenCalendar()

{

//关闭选择窗口

document.all.Calendar.style.visibility='hidden';

}

function ShowCalendar(InputBox)

{

var x,y,intLoop,intWeeks,intDays;

var DivContent;

var year,month,day;

//var o=document.getElementById(InputBox);

var o=InputBox;

var oid=o.id;

var thisyear; //真正的今年年份

if(!oid)oid=o.name;

thisyear=new getToday();

thisyear=thisyear.year;

today = o.value;

if(isDate(today))

today = new getStringDay(today);

else

today = new getToday();

//显示的位置

x=o.offsetLeft;

y=o.offsetTop;

while(o=o.offsetParent)

{

x+=o.offsetLeft;

y+=o.offsetTop;

}

document.all.Calendar.style.left=x+2;

document.all.Calendar.style.top=y+20;

document.all.Calendar.style.visibility="visible";

//下面开始输出日历表格(border-color:#9DBAF7)

DivContent="table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'";

DivContent+="tr";

DivContent+="td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'";

//年

DivContent+="select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'";

for (intLoop = thisyear - 100; intLoop (thisyear + 2); intLoop++)

DivContent+="option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + "" + intLoop + "/option";

DivContent+="/select";

//月

DivContent+="select name='Month' id='Month' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'";

for (intLoop = 0; intLoop months.length; intLoop++)

DivContent+="option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + "" + months[intLoop] + "/option";

DivContent+="/select";

DivContent+="/td";

DivContent+="td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA; font-weight:bold; font-family:Wingdings 2,Wingdings,Webdings; font-size:16px; padding-top:2px; color:#4477FF; cursor:hand' align='center' title='关闭' onClick='javascript:HiddenCalendar()'S/td";

DivContent+="/tr";

DivContent+="trtd align='center' colspan='2'";

DivContent+="table id='calendar' border='0' width='100%'";

//星期

DivContent+="tr";

for (intLoop = 0; intLoop days.length; intLoop++)

DivContent+="td align='center' style='font-size:12px'" + days[intLoop] + "/td";

DivContent+="/tr";

//天

for (intWeeks = 0; intWeeks 6; intWeeks++)

{

DivContent+="tr";

for (intDays = 0; intDays days.length; intDays++)

DivContent+="td onClick='GetDate(\"" + oid + "\")' style='cursor:hand; border-right:1px solid #BBBBBB; border-bottom:1px solid #BBBBBB; color:#215DC6; font-family:Verdana; font-size:12px' align='center'/td";

DivContent+="/tr";

}

DivContent+="/table/td/tr/table";

document.all.Calendar.innerHTML=DivContent;

newCalendar();

}

function isDate(dateStr)

{

var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;

var matchArray = dateStr.match(datePat);

if (matchArray == null) return false;

var month = matchArray[3];

var day = matchArray[5];

var year = matchArray[1];

if (month 1 || month 12) return false;

if (day 1 || day 31) return false;

if ((month==4 || month==6 || month==9 || month==11) day==31) return false;

if (month == 2)

{

var isleap = (year % 4 == 0 (year % 100 != 0 || year % 400 == 0));

if (day 29 || (day==29 !isleap)) return false;

}

return true;

}

/script

style type="text/css"

!--

td,input {

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: 12px;

}

--

/style

/head

body

form name="form1" method="post" action=""

table width="500" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC"

tr bgcolor="#FFFFFF"

td align="right"您的生日:/td

tdinput name="birthday" type="text" id="birthday" title="点击选择" onClick="javascript:ShowCalendar(this)" size="20"

input type="button" name="Submit" value="选 择" onClick="javascript:ShowCalendar(form1.birthday)"/td

tdBLOG:a href="";/a/td

/tr

/table

/form

/body

/html

如何用JavaScript编写一个万年历

html

head

base fptype="TRUE"

meta http-equiv="Content-Type" content="text/html; charset=gb2312"

title本地时间/title

scriptsetInterval("jnkc.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);/script

marquee scrollamount=8div id='jnkc'/div

/marquee

/head

body

script language=javascript

function settab()

{

var tab=document.getElementById('tab1');

//alert(se1.options[se1.selectedIndex].text)

var se11=se1.options[se1.selectedIndex].text;

var se21=se2.options[se2.selectedIndex].text;//下拉列表中的日期

//alert(se11+","+se21)

var lastday=new Date(se11,se21,0).getDate();//所选择月的天数

var firstday=new Date(se11,se21-1,1).getDay();//选择月的第一天是星期几

//alert(lastday+","+firstday)

var weekend;//每周的最后一天的日期

var lines;//第几行,相对于表格,星期为第零行

//第一行日期

for(var lin1=0;lin17;lin1++)

{

if(lin1firstday)

tab.rows[1].cells[lin1].innerHTML=" ";

else

if(lin1==0||lin1==6)

tab.rows[1].cells[lin1].innerHTML="font color=red"+(lin1-firstday+1)+"/font";

else

tab.rows[1].cells[lin1].innerHTML=lin1-firstday+1;

//alert(lin1+","+firstday)

weekend=lin1-firstday+1;

lines=1;//第一行

}

//将最后2行值空之后再添加数据,以免遗留上一个月的数据,二行相对于非闰年的二月份

for(var i=0;i7;i++)

{

tab.rows[5].cells[i].innerHTML=" ";

tab.rows[6].cells[i].innerHTML=" ";

}

while(weekendlastday)//循环到月末

{

for(var lin2=0;lin27;lin2++)

{

if(weekend+lin2lastday)//数字小于等于当前月的最后一天

{

if(lin2==0||lin2==6)

tab.rows[lines+1].cells[lin2].innerHTML="font color=red"+(weekend+1+lin2)+"/font";

else

tab.rows[lines+1].cells[lin2].innerHTML=weekend+1+lin2;

}

javascript怎么制作日历控件?

肯定你得设计一个日历控件的界面,关于界面的展现就需要用到CSS和html相关知识和内容了,为了好看,一般都会需要有素材图片。

界面上的设计这个仁者见仁不好说,但是一般都会有的元素有年份、月份选择栏,日期选择栏

还有一个选择之后,日期的值的展示对象(一般是input或者div元素对象na)

确定好这些必要元素后,你就可以考虑与这些对象相关的行为 有哪些,对应的方法是什么。

如果我来做,就会有一个总体的日历类calendar,他包含

init方法初始化(如动态创建日历控件的必要元素,和初始值等,根据已有日期展现控件界面)

dayChoose方法(用户的最终日期选择处理)

monthChoose方法(用户修改月份后界面的改变,界面中日期肯定有变动)

yearChooes方法(用户修改年份后,界面的改变,考虑闰年问题)

当然在具体做的过程中还能抽象出不少相对独立的方法过程。

最好你能去参考网上开源日历控件,看它们的代码,去理解它们的思路,以上想法是我个人简单设想,偏过程化,没有很好的使用面向对象的编程思想


网页题目:用javascript编写日历,js简单日历代码
文章分享:http://njwzjz.com/article/dscdsog.html