作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!