1

<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>");
    }
4

3 回答 3

5

与其<tr>在创建所有 s 后创建<td>s,我可以建议同时做这两个吗?

var count = 0, $row;

for (var i = 0 - dOW; i < dIM; i++) {
    var day = i + 1;
    if (count % 7 === 0) {
        $row = $('<tr />').appendTo('#calendar');
    }
    if (day > 0) {
        if (day === d) {
            $row.append("<td>[" + day + "]</td>");
        } else {
            $row.append("<td>" + day + "</td>");
        }
    } else {
        $row.append("<td>s</td>");
    }
    count++;
}
于 2013-01-17T01:32:03.427 回答
2

使用嵌套的 for 循环。

var dynamicTable= '<table cellspacing="10"';
for (i = 0; i < 10; i ++)
{
  dynamicTable+= "<tr>";
  var row = i;
  row += 1;
  for (j = 0; j < 10; j++)
  {
      var col = j;
      col += 1;
      dynamicTable += '<td>';
               dynamicTable += "x";// cell text
      dynamicTable += '</td>';
  }
  dynamicTable += '</tr>';
}
dynamicTable+= '</table>';
于 2013-01-17T01:35:46.110 回答
1

这是一个代码:

var arr=$("#calendar").val();
var htmlCode="";
int length=arr.length;
for(var i=0;i<length/7;i++)
{
   htmlCode+="<tr>";
   for(var j=i*7;j<i*7 + 7 ; j++) 
   {
      htmlCode+="<td>"+arr[j]+"</td>";
   }
   htmlCode+="</tr>";
}
于 2013-01-17T01:35:32.810 回答