简体   繁体   English

Javascript,DOM:appendChild和textContent

[英]Javascript, DOM: appendChild and textContent

I submitted a Firefox addon to the mozilla directory and it was rejected because of this: 我向Firefox目录添加了一个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>';

I spoke to the editor and he tells me to " use a text node and then insert it using appendChild, textContent, etc " 我与编辑交谈,他告诉我“ use a text node and then insert it using appendChild, textContent, etc

That sounds like total gibberish to me, i have never used TextContent, appendChild etc and am a bit lost. 对我来说,这听起来简直是胡言乱语,我从未使用过TextContent, appendChild等,并且有点迷失了。 Can you show me how to do the above and I'll edit the other 13 instances i have of something like the above in my script? 您能告诉我如何执行上述操作,然后我将在脚本中编辑类似上述内容的其他13个实例吗?

Thanks! 谢谢!

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. 我建议咨询MDN以确保了解FF。

He wants you to programmatically create the HTML in JavaScript. 他希望您以编程方式在JavaScript中创建HTML。 Take a read of this post , it should clear things up for you. 阅读这篇文章 ,它应该为您清除所有内容。 Post a comment if you would like more information. 如果您需要更多信息,请发表评论。

Rather than using innerHTML he wants you to use a function that changes the DOM in a more direct manner. 他希望您不使用innerHTML,而是使用一种以更直接的方式更改DOM的函数。 AppendChild AppendChild

Adds a node to the end of the list of children of a specified parent node. 将节点添加到指定父节点的子节点列表的末尾。 If the node already exists it is removed from current parent node, then added to new parent node 如果该节点已经存在,则将其从当前父节点中删除,然后添加到新的父节点中

https://developer.mozilla.org/En/DOM/Node.appendChild . https://developer.mozilla.org/En/DOM/Node.appendChild The format for it is: var child = element.appendChild(child); 其格式为: var child = element.appendChild(child);

In your instance i would do: 在您的情况下,我会这样做:

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

Where locationInDom is where you were going to use the innerHTML. locationInDom是您要使用innerHTML的位置。 If the innerHTML was going into a table then just add an id to the specific existing column and use the method getElementById and add at that point. 如果将innerHTML放入表中,则只需向现有的特定列中添加一个ID,然后使用getElementById方法并在该位置添加即可。

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

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