如何修复“无法读取属性'createDocumentFragment'”

时间:2019-05-15 09:56:00

标签: javascript matrix

我在代码中遇到无法解决的问题。 我收到以下错误:

  

无法读取未定义的属性'createDocumentFragment'-jquery-3.3.1.min.js:2

这是我的代码:

   $.ajax({
        url: '/ajax/getPvaSession.php'
    }).done(function (data) {
        let content = data['content'];
        console.log(content);
        $('body').append('<table id="table"></table>');
        for (let i=0;i<content[i].length;++i) {
            $('table').append('<tr id="' + i + '"></tr>');
            for (let j=0; j<content.length; ++j){
                $(i).append('<td id="' + i + '_' + j + '">' + content[i][j] + '</td>')
            }
        }
    })

我的console.log显示了这一点(它是一个简单的矩阵):

(8) [Array(31), Array(31), Array(31), Array(31), Array(31), Array(31), Array(31), Array(31)]
0: (31) ["Projet", "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"]
1: (31) ["Administrateur Réseau", "1", "1", "1", "1", "1", "", "", "1", "1", "0.5", "", "", "", "", "1", "1", "1", "1", "1", "", "", "", "1", "1", "1", "1", "", "", "1", "1"]
2: (31) ["Intercontrat", "", "", "", "", "", "", "", "", "", "0.5", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
3: (31) ["Formation", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
4: (31) ["Congés payés", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
5: (31) ["Congés sans solde", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
6: (31) ["Incentive", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
7: (31) ["Maladie", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
length: 8

我在线阅读到很多人以错误的方式使用this遇到此错误,但是我没有在任何地方使用它。

您能澄清一下吗?并尽可能解释该错误,以免下次发生错误。 谢谢!

1 个答案:

答案 0 :(得分:0)

您需要以content.length而不是content[0].length的形式遍历外部数组。并在内部数组中使用content[0].length

赞:

var content = [["Administrateur Réseau","1","1","1","1","1","","","1","1","0.5","","","","","1","1","1","1","1","","","","1","1","1","1","","","1","1"],["Intercontrat","","","","","","","","","","0.5","","","","","","","","","","","","","","","","","","","",""],["Formation","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Congés payés","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Congés sans solde","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Incentive","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Maladie","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]]

$('body').append('<table id="table"></table>');
for (let i = 0; i < content.length; ++i) {                       //Remove the [i]. This will loop thru all contents   
  var tr = $('<tr id="' + i + '"></tr>');                        //Create a tr element and store on a variable
  $('table').append(tr);
  for (let j = 0; j < content[i].length; ++j) {                  //Add the [i]. This will loop thru the inner array
    tr.append('<td id="' + i + '_' + j + '">' + content[i][j] + '</td>'); //Append the td string on tr variable
  }
}
td {
  border: 1px solid #dddddd;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

另一个选择是构造HTML字符串。并将其添加到正文中

var content = [["Administrateur Réseau","1","1","1","1","1","","","1","1","0.5","","","","","1","1","1","1","1","","","","1","1","1","1","","","1","1"],["Intercontrat","","","","","","","","","","0.5","","","","","","","","","","","","","","","","","","","",""],["Formation","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Congés payés","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Congés sans solde","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Incentive","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],["Maladie","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]]


var html = "";

html += "<table>";
for (let i = 0; i < content.length; ++i) {
  html += "<tr id='" + i + "'>";
  for (let j = 0; j < content[i].length; ++j) {
    html += '<td id="' + i + '_' + j + '">' + content[i][j] + '</td>';
  }
  html += "</tr>";
}
html += "</table>";

$('body').append(html);
td {
  border: 1px solid #dddddd;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>