我不确定如何称呼这个问题,所以欢迎任何修改!!!我正在努力学习保持第三方脚本以及我自己的脚本的干净 js 库结构。除了旧方法之外,除了将 js 文件添加到我的页面的基本方法外,我一无所知。
我在搜索时发现了这个,看到我不太擅长 javascript 或 jquery 这对我来说很有意义 https://github.com/volojs/create-template
我的问题是我的文件夹结构如下
- index.html
- Assets /js/lib/gsap/
- Assets /js/lib/bootstrap/
- assets/js/lib/whateverelse
在我的 index.html 文件中,我有指向 jquery 和名为 app.js 的文件的链接
但我对如何让它工作感到困惑,如何从目录中调用文件?
基本上我想从 1 个文件调用多个 js 文件
最佳答案
如果我理解正确,你需要 app.js 中包含的文件和 index.html 中包含的 app.js。
app.js
function dynamicallyLoadScript(url) {
var script = document.createElement("script"); // Make a script DOM node
script.src = url; // Set it's src to the provided URL
document.head.appendChild(script); // Add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead)
}
dynamicallyLoadScript('assets/js/lib/bootstrap/test.js');
dynamicallyLoadScript('assets/js/lib/gsap/test.js');
index.html
<script src="app.js"></script>
函数取自here . Guy 几乎回答了所有相关问题。
关于javascript - 使用 require 的库结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50767218/