【发布时间】:2022-01-15 09:36:03
【问题描述】:
我的文件结构简单,包含 HTML 和 JS 文件,如下所示:
templates/
- index.html
- menuselector.js
这是我要导入 HTML 的函数示例:
export function getTypeOfClient() {
const select = document.getElementById('inputGroupSelect02');
const value = select.options[select.selectedIndex].value;
if (value === '1') {
document.getElementById('person').style.display = "block";
document.getElementById('company').style.display = "none";
} else if (value === '2') {
document.getElementById('company').style.display = "block"
document.getElementById('person').style.display = "none";
} else {
document.getElementById('person').style.display = "none";
document.getElementById('company').style.display = "none";
}
}
以下是使用此函数的 HTML 代码示例:
<select class="form-select" id="inputGroupSelect03" onchange="getTypeOfClient()">
<option value="0" selected>Choose...</option>
<option value="1">Person</option>
<option value="2">Company</option>
</select>
当我在script 标签中编写代码时,一切正常,但我决定将此代码导出到 JS 文件中以使其更干净。
我将它导入到这样的文件中 - <script type="text/javascript" src="menuselector.js"></script>
这就是问题所在。每次在控制台中我得到 404 并没有找到这个文件。我试图在页眉、页脚、正文等中导入它,但它不起作用。我做错了什么?
【问题讨论】:
-
你试过
src="./menuselector.js"吗? -
404 表示找不到文件。用网络检查器检查你的文件路径,确保它是正确的。
标签: javascript html