JavaScript Implementation Calendar control (year-day closing button) _javascript tips

Source: Internet
Author: User
Tags getdate
Often use Google's friends must be vivid memories of Google Calendar control it, then we also to achieve a, although the function and effect is not comparable, but the important thing is the process of implementation.
here is the HTML structure to implement:
<div id= "A" ><div id= "head" ><span id= "Yface" > Years: <select id= "Year" ></select></span ><span id= "Mface" > Month: <select id= "month" ></select></span></div><div id= "Biaoti" ></div><div id= "Body" ></div></div>
First, the algorithm of the calendar query:
W=y+parseint (Y/4) +parseint (C/4) -2*c+parseint (26* (m+1)/10) +d-1;
The following is a detailed description of the process, interested can go to see:
Http://www.jb51.net/article/32572.htm
The following are the implemented JavaScript code:
Copy Code code as follows:

sx.activex.calender={
Bind:function (target) {
var a=document.createelement ("div");
var head=document.createelement ("div");
var biaoti=document.createelement ("div");
var select=document.createelement ("select");
var yface=document.createelement ("span");
var mface=document.createelement ("span");
var body=document.createelement ("div");
var select1=document.createelement ("select");
Yface.appendchild (select);
Mface.appendchild (Select1);
Head.appendchild (Yface);
Head.appendchild (Mface);
A.appendchild (head);
A.appendchild (Biaoti);
A.appendchild (body);
Yface.insertbefore (document.createTextNode ("year"), Yface.firstchild)
Mface.insertbefore (document.createTextNode ("month"), Mface.firstchild)
a.style.position= "Absolute";
Biaoti.style.height= "10%";
for (Var i=0;i<7;i++) {
var can=document.createelement ("span")
Can.style.width= "14%";
Can.style.height= "100%";
can.style.textalign= "center";
Biaoti.appendchild (CAN);
}
Biaoti.all[0].innertext= "Day"
biaoti.all[1].innertext= "One"
Biaoti.all[2].innertext= "Two"
Biaoti.all[3].innertext= "three"
biaoti.all[4].innertext= "Four"
Biaoti.all[5].innertext= "Five"
Biaoti.all[6].innertext= "Six"
Head.style.height= "20%";
a.style.position= "Absolute";
A.style.height= "200px";
A.style.width= "302px";
A.style.border= "1px red solid";
Yface.style.width= "50%";
Yface.style.padding= "5px";
Yface.style.height= "100%";
Select.style.width= "80%";
for (Var i=1960;i<2010;i++) {
var option=document.createelement ("option");
option.text=i;
Select.add (option);
}
Mface.style.width= "50%";
Mface.style.padding= "5px";
Mface.style.height= "100%";
Select1.style.width= "80%";
for (Var i=1;i<=12;i++) {
var option=document.createelement ("option");
option.text=i;
Select1.add (option);
}
Body.style.height= "70%";
for (Var i=0;i<42;i++) {
var span=document.createelement ("span");
Span.style.width= "14%";
Span.style.height= "16%";
span.style.textalign= "center";
Span.onmouseover=function () {
This.style.cursor= "Hand";
This.tempcolor=this.style.backgroundcolor;
This.style.backgroundcolor= "LightBlue";
}
Span.onmouseout=function () {
This.style.backgroundcolor=this.tempcolor;
}
Span.onclick=function () {
target.value=select.options[select.selectedindex].text+ "Year" +select1.options[select1.selectedindex].text+ "month" + This.innertext+ "Day";
A.parentnode.removechild (a);
}
Body.appendchild (span);
}
Select.onchange=function () {
for (Var o in Body.all) {
Body.all[o].innertext= "";
if (o.tostring ()!= "Length")
Body.all[o].style.backgroundcolor= "";
}
var Year1=this.options[this.selectedindex].text;
var Month1=select1.options[select1.selectedindex].text;
var y=parseint (Year1.substr (2,2)-0);
var c=parseint (Year1.substr (0,2));
var m=parseint (month1);
M=m>=3?m: (y=y-1,m+12);
var d=1;
var w=y+parseint (Y/4) +parseint (C/4) -2*c+parseint (26* (m+1)/10) +d-1;
if (w<0) w=w+700;
w=w%7;
Switch (parseint (month1)) {
Case 2:
if (parseint (year1)%4==0)
var r=29;
Else
var r=28;
var day=w;
for (Var d=1;d<=r;d++) {
Body.all[day++].innertext=d;
if (parseint (year1) = = (new date ()). getyear () && parseint (month1) = = (new date ()). GetMonth () +1 && d== (new Date ()). GetDate ())
Body.all[day-1].style.backgroundcolor= "Red";
body.all[41].innertext= "Close";
}
Break
Default

if (parseint (month1) ==1 | | parseint (month1) ==3 | | parseint (month1) ==5 | | parseint (month1) ==7 | | parseint (month1) ==8 | | parseint (month1) ==10 | | parseint (month1) ==12)
var r=31;
Else
var r=30;
var day=w;
for (Var d=1;d<=r;d++) {
Body.all[day++].innertext=d;
if (parseint (year1) = = (new date ()). getyear () && parseint (month1) = = (new date ()). GetMonth () +1 && d== (new Date ()). GetDate ())
Body.all[day-1].style.backgroundcolor= "Red";
body.all[41].innertext= "Close";
}
Break


}

}
Select1.onchange=function () {
for (Var o in Body.all) {
Body.all[o].innertext= "";
if (o.tostring ()!= "Length")
Body.all[o].style.backgroundcolor= "";
}
var Month1=this.options[this.selectedindex].text;
var Year1=select.options[select.selectedindex].text;
var y=parseint (Year1.substr (2,2)-0);
var c=parseint (Year1.substr (0,2));
var m=parseint (month1);
M=m>=3?m: (y=y-1,m+12);
var d=1;
var w=y+parseint (Y/4) +parseint (C/4) -2*c+parseint (26* (m+1)/10) +d-1;
if (w<0) w=w+700;
w=w%7;
Switch (parseint (month1)) {
Case 2:
if (parseint (year1)%4==0)
var r=29;
Else
var r=28;
var day=w;
for (Var d=1;d<=r;d++) {
Body.all[day++].innertext=d;
if (parseint (year1) = = (new date ()). getyear () && parseint (month1) = = (new date ()). GetMonth () +1 && d== (new Date ()). GetDate ())
Body.all[day-1].style.backgroundcolor= "Red";
body.all[41].innertext= "Close";
}
Break
Default

if (parseint (month1) ==1 | | parseint (month1) ==3 | | parseint (month1) ==5 | | parseint (month1) ==7 | | parseint (month1) ==8 | | parseint (month1) ==10 | | parseint (month1) ==12)
var r=31;
Else
var r=30;
var day=w;
for (Var d=1;d<=r;d++) {
Body.all[day++].innertext=d;
if (parseint (year1) = = (new date ()). getyear () && parseint (month1) = = (new date ()). GetMonth () +1 && d== (new Date ()). GetDate ())
Body.all[day-1].style.backgroundcolor= "Red";
body.all[41].innertext= "Close";
}
Break


}

}
var date=new date ();

for (Var s1=0;s1<select.options.length;s1++) {
if (parseint (select.options[s1].text) ==parseint (Date.getyear ())) {
Select.options[s1].selected=true;
Break
}
}
for (Var s2=0;s2<select1.options.length;s2++) {
if (parseint (select1.options[s2].text) ==parseint (Date.getmonth ()) +1) {
Select1.options[s2].selected=true;
Break
}
}
Select.onchange ();
for (var i in Body.all) {
if (Body.all[i].innertext==date.getdate ()) {
Body.all[i].style.backgroundcolor= "Red";
}
}
Target.onfocus=function () {
Document.body.appendChild (a);
a.style.left=target.offsetleft+ "px";;
a.style.top=target.offsettop+target.offsetheight+ "px";
}
Target.onblur=function () {
if (a && window.event.clienty>a.offsettop && window.event.clienty<a.offsettop+a.offsetheight && window.event.clientx>a.offsetleft && window.event.clientx<a.offsetleft+a.offsetwidth)
Return
if (!a) return;
A.parentnode.removechild (a);
}
body.all[41].innertext= "Close";
Body.all[41].onclick=function () {
This.style.backgroundcolor= "";
A.parentnode.removechild (a);

}
}
}

The entry parameter is the HTML object to bind, which is typically text input.
Here is the calling code:
<title>untitled document</title>
<body>
<script src= "Kongjian.js" ></script>
<input type= "text" id= "a" >
<script>
Sx.activex.calender.bind (document.getElementById ("a"));
</script>
</body>
Almost so, the code is more verbose, not very good, if a friend has a better way, please communicate with me a lot ah.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.