Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery函数没有';t按我认为的方式打印表格网格_Jquery_Html - Fatal编程技术网

jQuery函数没有';t按我认为的方式打印表格网格

jQuery函数没有';t按我认为的方式打印表格网格,jquery,html,Jquery,Html,我试图创建一个动态表格网格;然而,我尝试的功能,在我的非专业意见中,感觉它应该工作,但没有 var height = 2; // Determine <tr> amount var width = 10; // Determine <td> amount createTable(height, width); function createTable(height, width) { for (i = 0; i < height; i++) { $(

我试图创建一个动态表格网格;然而,我尝试的功能,在我的非专业意见中,感觉它应该工作,但没有

var height = 2; // Determine <tr> amount
var width = 10; // Determine <td> amount

createTable(height, width);

function createTable(height, width) {
  for (i = 0; i < height; i++) {
    $('#template').append('<tr>');
    for (j = 0; j < width; j++) {
      $('#template').append('<td></td>');
    }
    $('#template').append('</tr>');
  }
}
var高度=2;//确定数量
变量宽度=10;//确定数量
createTable(高度、宽度);
函数createTable(高度、宽度){
对于(i=0;i
这给了我一个html输出:

<table id="template" cellpadding="5" cellspacing="0">
  <tbody>
    <tr></tr>
    <tr></tr>
  </tbody>
  <td></td>
  <td></td> . . .
  . . . ~20 times?
</table>

. . .
. . . ~20次?
这就是我想要的样子:

<table id="template" cellpadding="5" cellspacing="0">
  <tbody>
    <tr>
      <td></td> (10 times)
    </tr>
    <tr>
      <td></td> (10 times)
    </tr>
  </tbody>
</table>

(10次)
(10次)

每次添加到
表时,您的逻辑都是错误的

FYI:jQuery
.append(“”
将自动追加结束标记,因为它不处理字符串,而是处理HTML DOM对象和元素

照办

function createTable(height, width) {
  var _html = "";
  for (i = 0; i < height; i++) {
    _html += '<tr>';
    for (j = 0; j < width; j++) {
       _html += '<td></td>';
    }
    _html += '</tr>';
    
  }
  $('#template').append(_html);
}
函数createTable(高度、宽度){
var_html=“”;
对于(i=0;i
更少的DOM操作。内存效率更高

var height=2;//确定数量
变量宽度=10;//确定数量
createTable(高度、宽度);
函数createTable(高度、宽度){
对于(i=0;i
您还希望它生成什么?您是否希望它们被附加到
tr
s下,即使您明确地将目标放在养育
元素上?您认为当前将
td
s放在
tr
s?2
的具体位置,其中包含10
包装?@roop,但它不应该附加到初始
调用之后的表元素吗?然后用
关闭?如果附加到
,那么它应该如何进入
tr
?附加的元素位于所选元素的内部。@这是逐段构建html。您正在一次完成整个事务/操作。@Roope-Yup。我现在明白了。非常感谢。您对我的实际问题的评论帮助我更好地理解了它。创建一个额外的类是不必要的。而且,这难道不会附加到每个
元素吗?因为他们都是同一类人。这只在我有1行的情况下有效?它将附加到与类名匹配的所有标记。
        var height = 2; // Determine <tr> amount
        var width = 10; // Determine <td> amount

        createTable(height, width);

        function createTable(height, width) {
          for (i = 0; i < height; i++) {
            $('#template').append('<tr class="dynTR">');
          }

          for (j = 0; j < width; j++) {
              $('.dynTR').append('<td></td>');
            }
        }