【发布时间】:2017-07-21 04:11:39
【问题描述】:
如何在脚本 type="module" 中获取 ownerDocument?
<template>
text
</template>
<script type="module">
let owner = document.currentScript.ownerDocument // is null
// But i need get <template>
let tpl = owner.querySelector('template')
</script>
【问题讨论】:
-
在 FF 上为我工作。不得不修复
</template>结束标签。 -
@Kaiido 它不适用于 Chrome 和其他浏览器
-
确实刚刚在金丝雀中尝试过,但它不起作用。我没有关于谁在这里的消息来源,但是 FF 在
DOMContentLoaded事件之前执行module脚本,而 chrome 在之后执行它......但最后,在这个例子中,owner === document。 -
所以在测试了更多的金丝雀之后,我倾向于在他们如何处理
defer脚本方面出现一个错误。它应该在之前DOMContentLoaded触发,但失败了。因此此时,currentScript是null -
我还没有找到如何实现你想要的。但是,我可以说所描述的行为是正确的。实际上,Edge 填写了相反的错误报告(设置了 currentScript)。 developer.microsoft.com/en-us/microsoft-edge/platform/issues/…
标签: html ecmascript-6 web-component es6-modules