3

我想创建一个 HTML div 网格,它具有相同的行数和列数,但行数/列数基于将传递给 Javascript 函数的特定数字。

例如,如果数字为 3,则网格将为 3 行和 3 列

如果数字是 4,网格将是 4 行和 4 列..等等

在 3 的情况下,输出的代码需要看起来像:

<div class="row">
  <div class="gridsquare">1</div>
  <div class="gridsquare">2</div>
  <div class="gridsquare">3</div>
</div>
<div class="row">
  <div class="gridsquare">4</div>
  <div class="gridsquare">5</div>
  <div class="gridsquare">6</div>
</div>
<div class="row">
  <div class="gridsquare">7</div>
  <div class="gridsquare">8</div>
  <div class="gridsquare">9</div>
</div>

什么是通过 javascript 进行迭代的好方法,以便可以识别正确的元素以添加行 div 开始或结束标签(?)

4

3 回答 3

8

这些方面的东西应该对你有用。

<html><head> 
  <script language="javascript"> 
    function genDivs(v){ 
      var e = document.body; // whatever you want to append the rows to: 
      for(var i = 0; i < v; i++){ 
        var row = document.createElement("div"); 
        row.className = "row"; 
        for(var x = 1; x <= v; x++){ 
            var cell = document.createElement("div"); 
            cell.className = "gridsquare"; 
            cell.innerText = (i * v) + x;
            row.appendChild(cell); 
        } 
        e.appendChild(row); 
      } 
      document.getElementById("code").innerText = e.innerHTML;

    }
  </script> 
</head> 
<body> 
    <input type="button" onclick="genDivs(6)" value="click me"> 
    <code id="code"></code>
</body> 
</html> 

结果:

  <div class="row">
    <div class="gridsquare">
      1
    </div>

    <div class="gridsquare">
      2
    </div>

    <div class="gridsquare">
      3
    </div>

    <div class="gridsquare">
      4
    </div>

    <div class="gridsquare">
      5
    </div>

    <div class="gridsquare">
      6
    </div>
  </div>

  <div class="row">
    <div class="gridsquare">
      7
    </div>

    <div class="gridsquare">
      8
    </div>

    <div class="gridsquare">
      9
    </div>

    <div class="gridsquare">
      10
    </div>

    <div class="gridsquare">
      11
    </div>

    <div class="gridsquare">
      12
    </div>
  </div>

  <div class="row">
    <div class="gridsquare">
      13
    </div>

    <div class="gridsquare">
      14
    </div>

    <div class="gridsquare">
      15
    </div>

    <div class="gridsquare">
      16
    </div>

    <div class="gridsquare">
      17
    </div>

    <div class="gridsquare">
      18
    </div>
  </div>

  <div class="row">
    <div class="gridsquare">
      19
    </div>

    <div class="gridsquare">
      20
    </div>

    <div class="gridsquare">
      21
    </div>

    <div class="gridsquare">
      22
    </div>

    <div class="gridsquare">
      23
    </div>

    <div class="gridsquare">
      24
    </div>
  </div>

  <div class="row">
    <div class="gridsquare">
      25
    </div>

    <div class="gridsquare">
      26
    </div>

    <div class="gridsquare">
      27
    </div>

    <div class="gridsquare">
      28
    </div>

    <div class="gridsquare">
      29
    </div>

    <div class="gridsquare">
      30
    </div>
  </div>

  <div class="row">
    <div class="gridsquare">
      31
    </div>

    <div class="gridsquare">
      32
    </div>

    <div class="gridsquare">
      33
    </div>

    <div class="gridsquare">
      34
    </div>

    <div class="gridsquare">
      35
    </div>

    <div class="gridsquare">
      36
    </div>
  </div>
于 2012-06-18T13:17:48.563 回答
2
var n=4;//take grid column value as you want

for(var i = 0; i < n; i++) {
    document.body.innerHTML+='<div class="row">';

    for(j = 0; j < n; j++) {
        document.body.innerHTML+='<div class="gridsquare">' + (i*5+j+1) + '</div>';
    }

    document.body.innerHTML+='</div>';
}
于 2012-06-18T13:02:33.283 回答
0
var grd="";
var rowCount=3;
for(var i=0; i<rowCount; i++){
/*here make whole string with row id to make identification of row then

您可以选择特定的行 */

}

$(yourelement).append(grd);

我认为这将是识别行的好方法

于 2012-06-18T13:02:37.183 回答