简体   繁体   English

Javascript:DOM,appendChild - 几个问题

[英]Javascript: DOM, appendChild - a couple of questions

I am just learning this stuff so please forgive if my code or questions are very basic. 我只是在学习这些东西所以请原谅我的代码或问题是否非常基础。

My "working code" (no errors etc) just so you know what I have so far: 我的“工作代码”(没有错误等)只是让你知道到目前为止我所拥有的:

var t = document.createElement("table"),
    tr = document.createElement("tr"),
    tr2 = document.createElement("tr"),
    td = document.createElement("td"),
    td2 = document.createElement("td"),
    a = document.createElement("a"),
    strong = document.createElement("strong");

t.style.width = "80%";
t.style.border = "0";
t.cellspacing = "2";
t.cellpadding = "2";

.

for (var i = 0; i < nodes.length; i++) {
    the_table_color = nodes[i].getAttribute("table_color");
    the_type = nodes[i].getAttribute("type");
    alt_link = nodes[i].getAttribute("alt_link");
    link_for_deletion = nodes[i].getAttribute("link_for_deletion");
    comment = nodes[i].getAttribute("comment");

    tr.bgColor = the_table_color;
    t.appendChild(tr);

    td.width = "16%";
    td.vAlign = "top";
    tr.appendChild(td);

    strong.appendChild(document.createTextNode(the_type));
    td.appendChild(strong);

    td.width = "16%";
    td.vAlign = "top";
    tr.appendChild(td);

    td2.width = "70%";
    td2.vAlign = "top";
    tr.appendChild(td2);

    a.href = alt_link;
    a.appendChild(document.createTextNode(alt_link));
    a.target = "_blank";
    td2.appendChild(a);
}

As you can see from above I have this bit: 从上面你可以看到我有这个:

td = document.createElement("td"),
    td2 = document.createElement("td"),

and the reason for that is, if I do not have the "td" declared again, the first one gets overwritten and the second one does not display... 原因是,如果我没有再次声明“td”,第一个被覆盖而第二个没有显示......
A) Is that the right way of doing this? A)这是正确的做法吗? (not sure if this is the right approach,I came about the solution of declaring another variable to get it to work, but it seems like extra code) (不确定这是否是正确的方法,我提出了声明另一个变量以使其工作的解决方案,但它似乎是额外的代码)

Then this line of code: 然后这行代码:

strong.appendChild(document.createTextNode(the_type));

just adds all the stuff from the loop in there :( 只是添加循环中的所有东西:(
For example of the first iteration of the for() loop has abc and the second had def it displays abcdef instead of abc the first time and def the second. 例如,for()循环的第一次迭代有abc而第二次有def它第一次显示abcdef而不是abc而第二次显示def。
B) Why is that and how do I fix it? B)为什么这样,我该如何解决?

Thanks! 谢谢!

Your for loop is executing against the same instance with each iteration. 每次迭代时,for循环都针对同一个实例执行。 You will not get multiple rows of data unless you perform your: 除非您执行以下操作,否则您将无法获得多行数据:

document.createElement("TR");
document.createElement("TD");

within the loop body. 在循环体内。 Otherwise, you're exactly right, you're just appending the same text to the same row and table cell each iteration through the loop. 否则,你是完全正确的,你只是在循环中每次迭代时将相同的文本附加到同一行和表格单元格。

It sounds like you need to change your approach to something like: 听起来你需要改变你的方法,如:

var table = document.createElement("TABLE");
for(var i = 0; i < nodes.length; i++){
    var tr = document.createElement("TR");
    var td = document.createElement("TD");
    tr.appendChild(td);
    table.appendChild(tr);

    // do something to to td    
}

Let me know if I've misunderstood your question, but if you're wondering why your table is building only one row or repeatedly modifying the same cell, that's definitely why... if there's more to your question let me know and I'll update the posting. 让我知道,如果我误解了你的问题,但如果你想知道为什么你的桌子只建一排或反复修改同一个单元格,这就是为什么......如果你的问题还有更多让我知道,我'将更新发布。

Happy coding. 快乐的编码。

B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM