如何在脚本ownerDocument“模块”中获得type=?
<template>
text
</template>
<script type="module">
let owner = document.currentScript.ownerDocument // is null
// But i need get <template>
let tpl = owner.querySelector('template')
</script>
发布于 2017-08-23 17:17:31
规范清楚地指出,在使用<script type="module">时,document.currentScript属性在执行期间被设置为null。参见“执行脚本块”下的规范。
当然,如果使用src属性,这是显而易见的。源无法知道脚本标记将调用它,而不是导入语句(或两者都调用)。当嵌入模块时,总是有一个脚本标记,但我的猜测是,他们想要使体验保持一致。
如果您的文档实际上是仍可作为全局文件使用的window.document。否则,如果仍然希望将脚本作为模块加载,则有两个选项:
https://stackoverflow.com/questions/45228821
复制相似问题