【问题标题】:After compiling from Dart to Javascript, document.querySelector can't find <body>从 Dart 编译到 Javascript 后,document.querySelector 找不到 <body>
【发布时间】:2014-02-08 11:11:44
【问题描述】:

编译为 javascript 后,当我尝试在 document 对象上查找带有 querySelector()&lt;body&gt; 元素时,我得到了 null。这是 Dart 代码:

void main() {
  print(document.querySelector('body')); // => null
}

这也会产生范围错误:

document.getElementsByTagName('body')[0] // => Range error, obviously the List is empty

不过,dart 代码在 Dartium 中运行良好。

【问题讨论】:

  • 我还应该补充一点,在 DOM 中肯定有一个 &lt;body&gt; 元素。

标签: javascript dart document dart2js


【解决方案1】:

想通了。我将脚本包含在&lt;head&gt; 部分中,并且应该在&lt;body&gt; 中的所有内容之后完成它。

【讨论】:

  • 对于需要等待文档被解析的脚本,应该使用defer。不幸的是,pub build 不会转换延迟飞镖脚本。让我去的事情嗯
【解决方案2】:

很奇怪,但 body 元素不需要 querySelector()。 只需使用document.body

【讨论】:

  • 还是不行,document.body 打印出null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2017-12-27
  • 1970-01-01
相关资源
最近更新 更多