English 中文(简体)
为什么表格 no了。 附录 儿童没有工作?
原标题:Why tables s td node.appendChild doesn t work?

我有这一 Java本功能,可以制作一个带有图像的桌子:

    function Draw(array) {
        // get the reference for the body
        var body = document.getElementsByTagName("body")[0];
        document.clear();

        // creates a <table> element and a <tbody> element
        var tbl = document.createElement("table");
        tbl.setAttribute("borderstyle", "1");
        var tblBody = document.createElement("tbody");

        // creating all cells
        for (var j = 0; j < 4; j++) {
            // creates a table row
            var row = document.createElement("tr");

            for (var i = 0; i < 4; i++) {
                // Create a <td> element and a text node, make the text
                // node the contents of the <td>, and put the <td> at
                // the end of the table row
                var cell = document.createElement("td");
                var cellText = document.createElement(array[4 * j + i]);
                cell.appendChild(cellText);
                row.appendChild(cell);
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }

        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
        // appends <table> into <body>
        body.appendChild(tbl);
        // sets the border attribute of tbl to 2;
        tbl.setAttribute("border", "2");
    }

但是

var cellText = document.createElement(array[4 * j + i]);
cell.appendChild(cellText);
row.appendChild(cell);

the cell.appendChild(cellText); doesn t work! I don t know why and I don t know how to resolve it!

update the a array is this:

    var a = Array(16);
    for (var i = 0; i < 16; i++) {
        a[i] =  <img src="  + i +  .jpg" /> ;
    }
最佳回答

www.un.org/spanish/ecosoc 最新答复

评论:

它只是提出一个案文。 这意味着,我看见<代码><img src ...而不是图像!

如果你告诉我们,array [4* j + i] 载于mark (例如列入有关实例)。

如果阵列包含markup,你不想制造任何新的 no。 而是指定到表格单位inner Rainbow/code>:

cell.innerHTML = array[4 * j + i];
row.appendChild(cell);

当你指定<代码>内/超文本时,浏览器将标识与内容相加。


Original answer before the comment below and before array s content was given:

为创建案文节点,您使用createTextNode,而不是createElement/code>。 因此:

// Change here ---------v
var cellText = document.createTextNode(array[4 * j + i]);
cell.appendChild(cellText);
row.appendChild(cell);

Suppose array[4*j + i],Hi There"document.createElement (array [4* j + i]) calls wasquest the DOM to establish an element with the tag name Hi There, actually the way that document.createElement( div ),quests it to establish an content with the tag name .

问题回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!