gpt4 book ai didi

javascript - 如何将 javascript 和 html 拆分为多个文件

转载 作者:行者123 更新时间:2023-12-02 19:50:14 25 4
gpt4 key购买 nike

我正在使用 PHP、HTML 和 jQuery。可以说这是我原来的工作“index.html”

<script type="text/javascript" src="jquery.js"></script>
<script>
var p1 = "This is part 1";
var p2 = "This is part 2";
$(document).ready(function(){
alert(p1);
$('#content').html(p2);
});
</script>
<div id="content"></div>
<div id="content2">Hello</div>

我想将代码分解为“index.html”和“otherFile.notSureWhatExt”,但它们的工作方式就像在同一个文件中一样。

index.html:

<script type="text/javascript" src="jquery.js"></script>
<script type="notSureWhatType" src="otherFile.notSureWhatExt"></script>
<script>
var p1 = "This is part 1";
$(document).ready(function(){
alert(p1);
});
</script>
<div id="content"></div>

otherFile.notSureWhatExt:

<script>
var p2 = "This is part 2";
$(document).ready(function(){
$('#content').html(p2);
});
</script>
<div id="content2">Hello</div>

使用jsp,我可以简单地使用<%@ include file="otherFile.anyCustomExtAlsoWorks"%>。但现在我不这样做。我想要的是当调用index.html时,我可以看到消息框“这是第1部分”和底部的2个div,即“这是第2部分”和“你好”。

最佳答案

您可以将 javascript 放入 .js 文件中。不需要脚本标签。

然后您可以将其包含在

<script type="text/javascript" src="otherFile.js"></script>

关于javascript - 如何将 javascript 和 html 拆分为多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9409466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com