TableElement table = new TableElement();
document.body.children.add(table);
table.children.add(new Element.html("<tr><td>test</td></tr>"));
上面的代码片段返回错误:“Uncaught Bad state: No element”。阅读 DartLang 网站上的 API 文档,这是因为在使用 Element.html("") 构造函数时,“HTML 片段被解析为好像它出现在 标记的上下文中”。因此,我们需要写:
new Element.html("<table><tr><td>test</td></tr></table>")
然而,阅读“dart in action”时,Chris Buckett 在构建表格时使用了完全相同的语法。
有什么改变还是我误解了什么?
最佳答案
这是浏览器的“功能”。一个 <tr>
元素不能单独存在,当它找到一个时就从 HTML 中删除。
文档至 new Element.html()
说
Creates an HTML element from a valid fragment of HTML.
或者,您可以使用
table.append(
new TableRowElement()..append(new TableCellElement()..text = 'test'));
DartPad example
关于dart - 在 Dart 中必须 element.html ("") 出现在 body 标签的上下文中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810272/