我<td>
使用以下代码创建了具有各种内容的各种标签:
for (var i = 0 - dOW; i < dIM; i++) {
var day = i + 1;
if (day > 0) {
if (day === d) {
$("#calendar").append("<td>[" + day + "]</td>");
} else {
$("#calendar").append("<td>" + day + "</td>");
}
} else {
$("#calendar").append("<td>s</td>");
}
}
结果:
ss 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [16] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
有没有办法将每 7 个元素包装在一个<tr>
标签中,这样 HTML 将是:
<tr>
<td>s</td>
<td>s</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
...
完整代码:
function calendar(d){
var t = new Date(d); // Today [Wed Jan 16 2013 00:00:00 GMT-0500 (EST)]
var d = t.getDate(); // Today's date (1-31) [16]
var y = t.getFullYear(); // Full year [2013]
var m = t.getMonth(); // Month (0-11) [0]
var mN = [ // Month name array (0-11)
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var fM = mN[m]; // Full month name [January]
var dIM = new Date(y, m + 1, 0).getDate(); // Number of days in current month (1-31) [31]
var dOW = new Date(y, m, 1).getDay(); // Day of the first day of the month (0-6) [2]
var nOW = Math.ceil((dIM + dOW) / 7); // Number of weeks in the month, including space [5]
for (var i = 0 - dOW; i < dIM; i++){
var day = i + 1;
if (day > 0){
if (day === d){
$("#calendar").append("<td>[" + day + "]</td>");
} else {
$("#calendar").append("<td>" + day + "</td>");
}
} else {
$("#calendar").append("<td>s</td>");
}
}
}
// "1 March 2013"
calendar(new Date());
到目前为止,我已经尝试将它分成两个for
循环,一个设置为行数,另一个设置为列,但这不允许我使用if
允许我放入间隔 div 的语句:
if (day === d) {
$("#calendar").append("<td>[" + day + "]</td>");
} else {
$("#calendar").append("<td>" + day + "</td>");
}