1

我向 mozilla 目录提交了一个 Firefox 插件,但由于以下原因被拒绝:

item.innerHTML =   '<table style="border-collapse: collapse; width: 100%;">'
                  + '<tr><td style="line-height: inherit; padding: 0pt; width: 100%;">'
                   + word.title
                   + '</td></tr>'
                    + '</table>';

我和编辑谈过了,他告诉我“ use a text node and then insert it using appendChild, textContent, etc

这对我来说听起来完全是胡言乱语,我从未使用过TextContent, appendChild等,有点迷失了。您能告诉我如何执行上述操作吗?我将在脚本中编辑其他 13 个类似上述内容的实例?

谢谢!

4

3 回答 3

5

You probably need to do something along these lines:

var t  = document.createElement("table"),
    tb = document.createElement("tbody"),
    tr = document.createElement("tr"),
    td = document.createElement("td");

    t.style.width = "100%";

    // note the reverse order of adding child        
    tr.appendChild(td);
    tb.appendChild(tr);
    t.appendChild(tb);

   // more code to populate table rows and cells
   item.appendChild(t);

Note: There are caveats to creating a table this way as things work differently between browsers. I would suggest consulting MDN to be sure about FF.

于 2011-07-28T21:37:22.057 回答
3

He wants you to programmatically create the HTML in JavaScript. Take a read of this post, it should clear things up for you. Post a comment if you would like more information.

于 2011-07-28T21:38:19.810 回答
2

他希望您使用一个以更直接的方式更改 DOM 的函数,而不是使用 innerHTML。追加子

将一个节点添加到指定父节点的子节点列表的末尾。如果节点已经存在,则从当前父节点中删除,然后添加到新父节点

https://developer.mozilla.org/En/DOM/Node.appendChild。它的格式是:var child = element.appendChild(child);

在你的例子中,我会这样做:

var newText = document.createTextNode(word.title);
locationInDom.appendChild(newText);

locationInDom 是您要使用 innerHTML 的位置。如果 innerHTML 正在进入一个表,那么只需将一个 id 添加到特定的现有列并使用 getElementById 方法并在该点添加。

于 2011-07-28T21:44:08.723 回答