Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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
这个Javascript代码是什么导致它在IE中正确生成表的?_Javascript_Internet Explorer_Html Table - Fatal编程技术网

这个Javascript代码是什么导致它在IE中正确生成表的?

这个Javascript代码是什么导致它在IE中正确生成表的?,javascript,internet-explorer,html-table,Javascript,Internet Explorer,Html Table,这个源代码是什么导致它在IE中实际生成一个表,而不是什么都不做 function generateATable() { tableContainer = document.getElementById("tableDiv"); var tableElement = document.createElement("table"); // Append the Table Element to

这个源代码是什么导致它在IE中实际生成一个表,而不是什么都不做

        function generateATable()
        {
            tableContainer = document.getElementById("tableDiv");
            var tableElement = document.createElement("table");

            // Append the Table Element to the table 
            // container.
            tableContainer.appendChild(tableElement);

            // IE Requires a TBODY when dynamically generating 
            // tables.  (I thought this was it but apparently it isn't)
            var tbodyElement = document.createElement("tbody");

            // First we'll append the tbody.
            tableElement.appendChild(tbodyElement);

            var trElement1 = document.createElement("tr");

            // Next we'll append the first trElement to the 
            // tbody.
            tbodyElement.appendChild(trElement1);

            var aaCell = trElement1.insertCell(-1);
            var abCell = trElement1.insertCell(0);

            var textNodeAA = document.createTextNode("AA");
            var textNodeAB = document.createTextNode("AB");

            aaCell.appendChild(textNodeAA);
            abCell.appendChild(textNodeAB);

            tbodyElement.appendChild(trElement1);

            var baCell = trElement1.cells[0].cloneNode(false);
            var bbCell = trElement1.cells[1].cloneNode(false);

            var textNodeBA = document.createTextNode("BA");
            var textNodeBB = document.createTextNode("BB");

            trElement2 = trElement1.cloneNode(false);

            tbodyElement.appendChild(trElement2);

            baCell.appendChild(textNodeBA);
            bbCell.appendChild(textNodeBB);

            trElement2.appendChild(baCell);
            trElement2.appendChild(bbCell);

            tableElement.style.border="4px solid black";

        }

我很抱歉…这是其他问题…创建表格的数据没有填写,所有人的选票都备份了…对不起

                    tableContainer = document.getElementById("tableDiv");
                    var tableElement = document.createElement("table");

                    // Append the Table Element to the table 
                    // container.
                    tableContainer.appendChild(tableElement);
就这样。其余的实际上是填充表

我们在现有HTML文档中找到一个名为“tableDiv”的div,然后创建一个元素,并将其添加到tableDiv中

document.createElement("table")
  ...
tableContainer.appendChild(tableElement);

这将向DOM添加一个元素。

创建可在页面上看到的表包括两个步骤-创建表节点:

var tableElement = document.createElement("table");
…并将其添加为文档中节点的子节点:

tableContainer = document.getElementById("tableDiv");
tableContainer.appendChild(tableElement);

我不知道你为什么对所有回答正确的人投反对票。您想逐行描述代码正在执行的操作吗

function generateATable() {
    /* obtain a reference to a div */
    tableContainer = document.getElementById("tableDiv");
    /* create a table element */
    var tableElement = document.createElement("table");

    // Append the Table Element to the table 
    // container.
    /* append the table element to the div */
    tableContainer.appendChild(tableElement);

    // IE Requires a TBODY when dynamically generating 
    // tables.  (I thought this was it but apparently it isn't)
    /* create a tbody element */
    var tbodyElement = document.createElement("tbody");

    // First we'll append the tbody.
    /* append the tbody element to the table element */
    tableElement.appendChild(tbodyElement);

    /* create a row element */
    var trElement1 = document.createElement("tr");

    // Next we'll append the first trElement to the 
    // tbody.
    /* append the row element to the tbody element */
    tbodyElement.appendChild(trElement1);

    /* insert two cells */
    var aaCell = trElement1.insertCell(-1);
    var abCell = trElement1.insertCell(0);

    /* create two text nodes */
    var textNodeAA = document.createTextNode("AA");
    var textNodeAB = document.createTextNode("AB");

    /* append the text nodes to the cells */
    aaCell.appendChild(textNodeAA);
    abCell.appendChild(textNodeAB);

    /* append the row element to the tbody element ... again */
    tbodyElement.appendChild(trElement1);

    /* create two new cells that are shallow copies of the two cells above */
    var baCell = trElement1.cells[0].cloneNode(false);
    var bbCell = trElement1.cells[1].cloneNode(false);

    /* create two more text nodes */
    var textNodeBA = document.createTextNode("BA");
    var textNodeBB = document.createTextNode("BB");

    /* create a row element that is a shallow copy of the first row */
    trElement2 = trElement1.cloneNode(false);

    /* append the 2nd row element to the tbody element */
    tbodyElement.appendChild(trElement2);

    /* append the text nodes to the cells */
    baCell.appendChild(textNodeBA);
    bbCell.appendChild(textNodeBB);

    /* append the cells to the 2nd row element */
    trElement2.appendChild(baCell);
    trElement2.appendChild(bbCell);

    /* set the border style of the table element */
    tableElement.style.border="4px solid black";
}

不,只是在内存中创建一个元素。要将它放到页面上,您必须将它附加到页面上已经存在的另一个元素,或者执行document.write.quibbling-他指出了什么是createElement和appendChild,这才是最重要的。我非常确定,它的内容不止这些……因为您不能直接将子元素附加到,您必须调用insertCell来创建一个“”元素。另外,在我看来,首先需要将tbody附加到表中……但必须有更多的内容,但OP正在做所有这些事情。leeand00:代码正在做你说的所有事情,它将tbody附加到表中,tr附加到tbody,插入单元格,将文本添加到这些单元格中,并将这些单元格添加到tr中。这就是Yuliy说的意思:“就是这样。”。我很高兴我没有回答这个问题,如果OP只是对每一个他神秘地认为不正确的回答投了否决票……你如何对付这种行为?这个问题本身不值得投否决票,也不值得冒犯。。。