這篇文章主要介紹js如何實(shí)現(xiàn)每日簽到功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)麻章免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
JS是JavaScript的簡(jiǎn)稱,它是一種直譯式的腳本語言,其解釋器被稱為JavaScript引擎,是瀏覽器的一部分,主要用于web的開發(fā),可以給網(wǎng)站添加各種各樣的動(dòng)態(tài)效果,讓網(wǎng)頁更加美觀。
具體內(nèi)容如下
js:
var calUtil = { getDaysInmonth : function(iMonth, iYear){ //當(dāng)前年月的總天數(shù) var dPrevDate = new Date(iYear, iMonth, 0); return dPrevDate.getDate(); }, bulidCal : function(iYear, iMonth) { //構(gòu)建當(dāng)前年月對(duì)應(yīng)的日歷 var aMonth = new Array(); aMonth[0] = new Array(7); aMonth[1] = new Array(7); aMonth[2] = new Array(7); aMonth[3] = new Array(7); aMonth[4] = new Array(7); aMonth[5] = new Array(7); aMonth[6] = new Array(7); var dCalDate = new Date(iYear, iMonth - 1, 1); var iDayOfFirst = dCalDate.getDay(); var iDaysInMonth = calUtil.getDaysInmonth(iMonth, iYear); var iVarDate = 1; var d, w; aMonth[0][0] = "日"; aMonth[0][1] = "一"; aMonth[0][2] = "二"; aMonth[0][3] = "三"; aMonth[0][4] = "四"; aMonth[0][5] = "五"; aMonth[0][6] = "六"; for (d = iDayOfFirst; d < 7; d++) { aMonth[1][d] = iVarDate; iVarDate++; } for (w = 2; w < 7; w++) { for (d = 0; d < 7; d++) { if (iVarDate <= iDaysInMonth) { aMonth[w][d] = iVarDate; iVarDate++; } } } return aMonth; }, ifHasSigned : function(signList,day){ var signed = false; $.each(signList,function(index,item){ var date = new Date(item.signDate); if(date.getDate() == day) { signed = true; return false; } }); return signed ; }, drawCal : function(iYear, iMonth ,signList) { var currentYearMonth = iYear+"年"+iMonth+"月"; var myMonth = calUtil.bulidCal(iYear, iMonth); var htmls = new Array(); htmls.push("<div class='sign_main' id='sign_layer'>"); htmls.push("<div class='sign_succ_calendar_title'>"); htmls.push("<div class='calendar_month_span'>"+currentYearMonth+"</div>"); htmls.push("</div>"); htmls.push("<div class='sign' id='sign_cal'>"); htmls.push("<table class='table'>"); htmls.push("<tr>"); htmls.push("<th>" + myMonth[0][0] + "</th>"); htmls.push("<th>" + myMonth[0][1] + "</th>"); htmls.push("<th>" + myMonth[0][2] + "</th>"); htmls.push("<th>" + myMonth[0][3] + "</th>"); htmls.push("<th>" + myMonth[0][4] + "</th>"); htmls.push("<th>" + myMonth[0][5] + "</th>"); htmls.push("<th>" + myMonth[0][6] + "</th>"); htmls.push("</tr>"); var d, w; for (w = 1; w < 7; w++) { htmls.push("<tr>"); for (d = 0; d < 7; d++) { var ifHasSigned = calUtil.ifHasSigned(signList,myMonth[w][d]); if(ifHasSigned){ htmls.push("<td class='on'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>"); } else { htmls.push("<td>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "</td>"); } } htmls.push("</tr>"); } htmls.push("</table>"); htmls.push("</div>"); htmls.push("</div>"); return htmls.join(''); } };
頁面效果代碼:
<style type="text/css"> @media screen and (min-width:1024px) { .rich_media { width: 500px; margin-left: auto; margin-right: auto; padding: 20px; } } </style> </head> <body > <div class="rich_media"> <div id="page-content"> <div > <span >每日簽到</span> <input type="hidden" id="userId" value="${user.id }" /> </div> <div class="container-fluid"> <div class="row-fluid" id="calendar"> </div> <div id="btnDiv" > <div class="row-fluid text-center"> <span id="sing_for_number" class="btn btn-default">簽到</span> </div> </div> </div> </div> </div> </body>
js調(diào)用方法
var str = calUtil.drawCal(current.getFullYear(),current.getMonth() + 1,signList); $("#calendar").html(str);
說明:signList是后臺(tái)查詢的已簽到的時(shí)間集合,傳入到j(luò)s方法中會(huì)去判斷哪一天簽到了,然后改變簽到天的顯示效果,如上圖!
此簽到頁面同樣適應(yīng)手機(jī)瀏覽器喲!
提要:
本文以寫當(dāng)前時(shí)間環(huán)境下當(dāng)月的日歷表為例,用最簡(jiǎn)單的方法實(shí)現(xiàn)JavaScript日歷,旨在展示JS世界中實(shí)用為本、簡(jiǎn)單為上的解決問題的思路。
Web頁中的日歷一般離不開表格,通常都使用表格裝載指定月的日期等信息。所以,要編寫JS日歷,首先必須解決的問題是表格的行與列問題。列是固定的,七列,因?yàn)橐恢苡衅咛?。行需要?jiǎng)討B(tài)計(jì)算,因?yàn)?,每一個(gè)月的第一天是星期幾是一個(gè)變數(shù),因而第一天在表格中的第幾個(gè)單元也就跟著變化,同時(shí),每個(gè)月的總天數(shù)不一致也影響著各個(gè)月對(duì)表格行數(shù)的需要量。
一. 表格的行數(shù)問題
1.首先取得處理月的總天數(shù)
JS不提供此參數(shù),我們需要計(jì)算??紤]到閏年問題會(huì)影響二月份的天數(shù),我們先編寫一個(gè)判斷閏年的自編函數(shù):
function is_leap(year) { return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0)); }
接著定義一個(gè)包含十二個(gè)月在內(nèi)的月份總天數(shù)的數(shù)組:
m_days=new Array(31,28+is_leap(ynow),31,30,31,31,30,31,30,31,30,31);
m_days這個(gè)數(shù)組里,二月份的天數(shù)已經(jīng)加入閏年的信息:28+is_leap(ynow)。數(shù)組元素從0開始,正好對(duì)應(yīng)于JS提供的Date函數(shù)提供的getMonth返回值,即0表示一月,1表示二月,2表示三月,依此類推。
這樣,各月總數(shù)可以這樣取得:m_days[x]。其中,x為0至11的自然數(shù)。
2.計(jì)算處理月第一天是星期幾
可以使用Date函數(shù)的getDay取得,返回的值從0到6,0表示星期一,1表示星期二,2表示星期三,其余依此類推。代碼如下(假設(shè)要處理的時(shí)間為2008年3月):
n1str=new Date(2008,3,1); firstday=n1str.getDay();
有了月總天數(shù)和該月第一天是星期幾這兩個(gè)已知條件,就可以解決表格所需行數(shù)問題:(當(dāng)前月天數(shù)+第一天是星期幾的數(shù)值)除以七。表格函數(shù)需要整數(shù),因此,我們使用Math.ceil來處理:
tr_str=Math.ceil((m_days[mnow] + firstday)/7);
表格中的tr標(biāo)簽實(shí)際上代表表格的行,因此變量tr_str是我們往下寫表格的重要依據(jù)。
二. 打印日歷表格
可以使用兩個(gè)for語句嵌套起來實(shí)現(xiàn):外層for語句寫行,內(nèi)層for語句寫單元格。
for(i=0;i<tr_str;i++) { //外層for語句 - tr標(biāo)簽 document.write("<tr>"); for(k=0;k<7;k++) { //內(nèi)層for語句 - td標(biāo)簽 idx=i*7+k; //表格單元的自然序號(hào) date_str=idx-firstday+1; //計(jì)算日期 //這里是處理有效日期代碼 } //內(nèi)層for語句結(jié)束 document.write("</tr>"); } //外層for語句結(jié)束
單元格的自然序號(hào)是否代表有效日期非常關(guān)鍵,為此必須加入一個(gè)過濾機(jī)制:僅打印有效的日期。有效的日期大于0小于小于等于處理月的總天數(shù)。
三. 以下是完整的JS日歷代碼:
function is_leap(year) { return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0)); } //是否為閏年 var nstr=new Date(); //當(dāng)前Date資訊 var ynow=nstr.getFullYear(); //年份 var mnow=nstr.getMonth(); //月份 var dnow=nstr.getDate(); //今日日期 var n1str=new Date(ynow,mnow,1); //當(dāng)月第一天Date資訊 var firstday=n1str.getDay(); //當(dāng)月第一天星期幾 var m_days=new Array(31,28+is_leap(ynow),31,30,31,30,31,31,30,31,30,31); //各月份的總天數(shù) var tr_str=Math.ceil((m_days[mnow] + firstday)/7); //表格所需要行數(shù) //打印表格第一行(有星期標(biāo)志) document.write ("<table border='1' align='center' width='220' cellspacing='0'><tr><td align='center'>日</td><td align='center'>一</td><td align='center'>二</td><td align='center'>三</td><td align='center'>四</td><td align='center'>五</td><td align='center'>六</td></tr>"); for(i=0;i<tr_str;i++) { //表格的行 document.write("<tr>"); for(k=0;k<7;k++) { //表格每行的單元格 idx=i*7+k; //單元格自然序列號(hào) date_str=idx-firstday+1; //計(jì)算日期 (date_str<=0 || date_str>m_days[mnow]) ? date_str=" " : date_str=idx-firstday+1; //過濾無效日期(小于等于零的、大于月總天數(shù)的) //打印日期:今天底色為紅 date_str==dnow ? document.write ("<td align='center' bgcolor='red'>" + date_str + "</td>") : document.write ("<td align='center'>" + date_str + "</td>"); } document.write("</tr>"); //表格的行結(jié)束 } document.write("</table>"); //表格結(jié)束
以上是“js如何實(shí)現(xiàn)每日簽到功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:js如何實(shí)現(xiàn)每日簽到功能
標(biāo)題來源:http://aaarwkj.com/article34/ihhhpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、關(guān)鍵詞優(yōu)化、用戶體驗(yàn)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)