2014-02-06 109 views
0

我試圖讓我的應用程序隱藏週末日子,我的日曆存儲在一個字符串中,然後打印到innerHTML。我將如何去隱藏週六和週日。日曆,需要一種方法來隱藏星期六和星期日?

我的日曆打印類似如下:

星期日星期一星期二星期三星期四星期五星期六

代碼:

calendarString = ''; 
calendarString += '<table width="100%" height="425" data-role="table" border="1" bordercolor="#c0c0c0" cellpadding="0" cellspacing="0">'; 
calendarString += '<tr>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Sun<\/td>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Mon<\/td>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Tue<\/td>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Wed<\/td>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Thu<\/td>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Fri<\/td>'; 
calendarString += '<td bgcolor=\"#DDDDDD\" align=\"center\" valign=\"center\" width=\"40\" height=\"22\">Sat<\/td>'; 
calendarString += '<\/tr>'; 
thisDate == 1; 

for(var i = 1; i <= 6; i++){ 
    calendarString += '<tr>'; 
    for (var x = 1; x <= 7; x++){ 
     daycounter = (thisDate - firstDay)+1; 
     thisDate++; 
     if((daycounter > numbDays) || (daycounter < 1)) { 
      calendarString += '<td align=\"center\" bgcolor=\"#888888\" height=\"30\" width=\"40\">&nbsp;<\/td>'; 
     } else { 
      if (checkevents(daycounter,monthNum,yearNum,i,x) || ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum))){ 
       if ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum)){ 
        tempweek = i; 
        tempdayofweek = x; 
        calendarString += '<td style=\"vertical-align: top;\" bgcolor=\"#AAFFAA\" height=\"30\" width=\"40\" onClick=showevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',' + i + ',' + x + ')>' + daycounter + '<\/td>'; 
       } else 
        calendarString += '<td style=\"vertical-align: top;\" bgcolor=\"#FFFFC8\" height=\"30\" width=\"40\" onClick=showevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',' + i + ',' + x + ')>' + daycounter + '<div id="amountevent" style="padding-left:5px;max-height:8px; height:8px; width:30px; max-width:30px;white-space: nowrap;">' + displayeventnumber(daycounter, monthNum, yearNum) + '</div><\/td>';   
      } else 
       calendarString += '<td style=\"vertical-align: top;\" bgcolor=\"#f9f9f9\" height=\"30\" width=\"40\">' + daycounter + '<\/td>'; 
     } 
    } 
    calendarString += '<\/tr>'; 
} 
calendarString += '<\/table>'; 
var object=document.getElementById('calendar'); 
object.innerHTML= calendarString; 
thisDate = 1; 

回答

2

添加類=「週末」的表格單元格屬性爲週日和星期六。然後使用JavaScript來設置這個類的CSS樣式:

.weekend {display: none;} 

更新: 更普遍的,如果你的表看起來是這樣:

<table class="calendar"> 
    <tr><td class="weekend">Sunday</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td class="weekend">Saturday</td></tr> 
    <tr><td class="weekend"></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td class="weekend">6</td></tr> 
    ... 
    <tr><td class="weekend">28</td><td>29</td><td>30</td><td></td><td></td><td></td><td class="weekend"></td></tr> 
</table> 

<button type="button" onclick="hideWeekends()">Hide Weekends</button> 

和風格:

table.calendar {width: 100%} 

而且javascript:

function hideWeekends() { 
    // apply display: none style to elements with classname of "weekend". I recommend using jQuery if available. 
} 
+0

這種作品,它隱藏了我的星期日和星期六的標題,但不對整行做任何事情。 – Aero204

+0

你想要對行做什麼?或者你的意思是專欄?如果您希望它隱藏所有星期日和星期六元素,請務必在這些表單元格上添加該類。 (例如,當date.getDay()返回0或6) – Coenwulf

+0

我的意思是列,這將是最好的: 隱藏列或 重新運行腳本,這意味着我可以使表全屏(當我隱藏它們它不會使桌子全屏。 – Aero204

相關問題