【问题标题】:In Dart must element.html(" ") occur within the context of a body tag?在 Dart 中 element.html(" ") 必须出现在 body 标签的上下文中吗?
【发布时间】:2016-04-23 11:33:46
【问题描述】:
TableElement table = new TableElement();
document.body.children.add(table);
table.children.add(new Element.html("<tr><td>test</td></tr>"));

上面的代码sn-p返回错误:“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 在构建表格时使用了完全相同的语法。

有什么变化还是我误解了什么?

【问题讨论】:

    标签: dart dart-html


    【解决方案1】:

    这是一个浏览器“功能”。 &lt;tr&gt; 元素不能单独存在,只要找到它就会从 HTML 中删除。

    new Element.html() 的文档说

    从有效的 HTML 片段创建一个 HTML 元素。

    你也可以使用

      table.append(
          new TableRowElement()..append(new TableCellElement()..text = 'test'));
    

    DartPad example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-15
      • 2011-07-26
      • 2014-10-27
      • 2016-11-13
      • 2013-12-27
      • 1970-01-01
      • 2018-09-19
      • 2017-08-07
      相关资源
      最近更新 更多