javascript - 如何动态加载html文件并执行其中的外部js

标签 javascript jquery html

我有一个 html 文件:index.html,其中包含一些基本的 js 库。 我还有一些模块:

a.html:

<div id='a'>
  <script src="a1.js"></script>
  <script src="a2.js"></script>
</div>

a1.js:

console.log('a1')

a2.js:

console.log('a2')

如何动态加载a.html到index.html中并获取a1.js和a2.js执行完成的事件?

最佳答案

使用 w3data.js 来执行此操作 ( w3data.js )

示例:在index.html

<html>
<script src="http://www.w3schools.com/lib/w3data.js"></script>

<body>

<div w3-include-html="a.html"></div> 

<script>
w3IncludeHTML();
</script>

</body>
</html>

关于javascript - 如何动态加载html文件并执行其中的外部js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41085309/

相关文章:

javascript - 如何使用jquery选择选项功能

javascript - 如何访问/替换 Chrome 应用程序中的 iframe?

android - android 是否在提交按钮周围添加了额外的 Activity 区域?

jquery - 展开 Owl Carousel 内的项目

javascript - 如何在 JavaScript 中将单独的元素添加到列表中?

javascript - 在 React-Three-Fiber 中渲染索引缓冲区几何

php - 带有 PHP 身份验证脚本的 Javascript 登录页面 - 哪种实现最好?

jquery-ui 自动完成 : set source by callback function not working

javascript - 按我的 json 对象分组并获取每个组的计数

javascript - 滚动到具有特定类的 id