>  기사  >  웹 프론트엔드  >  자바스크립트로 달력 제어(년,월,일 닫기 버튼) 구현_javascript 기술

자바스크립트로 달력 제어(년,월,일 닫기 버튼) 구현_javascript 기술

WBOY
WBOY원래의
2016-05-16 17:46:441389검색

구글을 자주 사용하는 친구들은 아직도 구글의 화려한 캘린더 컨트롤을 기억하고 있으니 기능이나 효과는 비교할 수 없지만 중요한 것은
다음은 HTML 구조를 구현하는 것입니다. 🎜>:

< ;div id= "biaoti">

먼저 달력 쿼리 알고리즘에 대해 이야기해 보겠습니다.
w= y parseInt( y/4) parsInt(c/4)-2*c parsInt(26*(m 1)/10) d-1;
자세한 설명 과정은 다음과 같습니다.

http://www.jb51.net/article/32572.htm
다음은 구현된 자바스크립트 코드입니다:

코드 복사 코드는 다음과 같습니다.

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("선택");
var yface=document.createElement("span");
var mface=document.createElement("span");
var body=document.createElement("div");
var select1=document.createElement("선택");
yface.appendChild(선택);
mface.appendChild(select1);
head.appendChild(yface);
head.appendChild(mface);
a.appendChild(머리);
a.appendChild(biaoti);
a.appendChild(본문);
yface.insertBefore(document.createTextNode("年 "),yface.firstChild)
mface.insertBefore(document.createTextNode("月 "),mface.firstChild)
a.style.position=" 순수한";
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="日"
biaoti.all[1].innerText="一"
biaoti.all[2].innerText="two"
biaoti.all[3].innerText="三"
biaoti.all[4].innerText="4"
biaoti.all[5].innerText="五"
biaoti.all [6].innerText="六"
head.style.height="20%";
a.style.position="절대";
a.style.height="200px";
a.style.width="302px";
a.style.border="1px 빨간색 단색";
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(옵션);
}
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(옵션);
}
body.style.height="70%";
for(var i=0;i<42;i ){
varspan=document.createElement("span");
span.style.width="14%";
span.style.height="16%";
span.style.textAlign="center";
span.onmouseover=function(){
this.style.cursor="손";
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 "년" select1.options[select1.selectedIndex].text "월"입니다. innerText "일";
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=yparseInt(y/4)parseInt(c/4)-2*cparseInt(26*(m 1)/10)d-1 ;
if(w<0) w=w 700;
w=w%7;
switch(parseInt(month1)){
사례 2:
if(parseInt(year1)%4==0)
var r=29;
그 외
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="关闭";
}
휴식;
기본값:

if(parseInt(month1)==1 ||parseInt(month1)==3 ||parseInt(month1)==5 ||parseInt(month1)==7 ||parseInt (월1)==8 || 구문 분석(월1)==10 || 구문 분석(월1)==12)
var r=31;
그 외
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="关闭";
}
휴식;


}

}
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=yparseInt(y/4)parseInt(c/4)-2*cparseInt(26*(m 1)/10)d-1 ;
if(w<0) w=w 700;
w=w%7;
switch(parseInt(month1)){
사례 2:
if(parseInt(year1)%4==0)
var r=29;
그 외
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="关闭";
}
휴식;
기본값:

if(parseInt(month1)==1 ||parseInt(month1)==3 ||parseInt(month1)==5 ||parseInt(month1)==7 ||parseInt (월1)==8 || 구문 분석(월1)==10 || 구문 분석(월1)==12)
var r=31;
그 외
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="关闭";
}
휴식;


}

}
var date=new Date();

for(var s1=0;s1if(parseInt(select.options[s1].text)==parseInt(date.getYear()) ){
select.options[s1].selected=true;
휴식;
}
}
for(var s2=0;s2if(parseInt(select1.options[s2].text)==parseInt(날짜 .getMonth()) 1){
select1.options[s2].selected=true;
휴식;
}
}
select.onchange();
for(var i in body.all){
if(body.all[i].innerText==date.getDate()){
body.all[i].style.BackgroundColor=" 빨간색";
}
}
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.clientYreturn;
if(!a) return;
a.parentNode.removeChild(a);
}
body.all[41].innerText="关闭";
body.all[41].onclick=function(){
this.style.BackgroundColor="";
a.parentNode.removeChild(a);

}
}
}

入口参数是要绑定的html对象,这里一般是텍스트 입력.
하측면是调代码:

<머리>
제목 없는 문서

<본문>


<스크립트>
sx.activex.calender.bind(document.getElementById("a"));



差不多就这样, 代码比较冗长,不是很好,如果哪位朋友有更好的办法, 请与我多交流啊.
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:Node.js는 모달 창을 닫고 상위 페이지를 새로 고치거나 page_javascript 기술로 이동합니다.다음 기사:Node.js는 모달 창을 닫고 상위 페이지를 새로 고치거나 page_javascript 기술로 이동합니다.

관련 기사

더보기