javascript - 再次加载内容时,与 Fetched 部分一起动态加载的 ES6 模块不会执行
问题描述
我有一个使用 fetch API 切换内容的页面。每个内容都附带一个 js 文件。其中一些是 type='module'。然而,尽管模块在第一次加载期间运行良好,但当我切换内容(调用不同的 fetch 端点)并再次切换回来时,相应的脚本似乎不起作用。当整个页面发生变化时(例如,进入个人资料页面,然后再次进入主页),脚本正常工作。
---在主页内调用内容的部分---
...
auth.fetch(partUri).then((resp) => {
return resp.text().then(text => {
$('.title > h2').text(titles[ch - 1]);
$('#part-' + partNo).html(text);
}
});
});
...
--- 在内容的 html 文件中 ---
...
<script> <--- this script is working after switching back
new SimpleLightbox('.gallery-item a', {
disableScroll: false
});
</script>
<script type="module" src="js/part-3.js"></script> <--- this script is working only the first time
解决方案
推荐阅读
- javascript - react-jsonschema-form 中布尔字段的自定义字段验证
- r - 使用正则表达式在特定位置插入一些字符
- sockets - Unix Domain Socket Server 仅在客户端关闭时打印消息
- ansible - 如何在ansible playbook中比较来自多个主机的事实
- html - TD 扩展到绝对定位内容的大小
- python-3.x - 将 ThreadPoolExecutor 与一名工作人员一起使用是个好主意吗?
- android-studio - 无法在导入的颤振项目中使用特定图标
- apache-spark - 在 Cassandra 中使用 Spark 插入数据
- c - 没有错误时 Visual Studio 不显示警告 (C++)
- swagger - 将 API Gateway Cloudformation 模板转换为 Swagger 文件