如何将JavaScript代码从HTML文件中分离出来

<html><head><title>三种常用的对话框</title><scripttype="text/javascript">functionalertDialog(... <html> <head> <title>三种常用的对话框</title> <script type="text/javascript"> function alertDialog(){ alert("您成功实行了这个操作"); } function confirmDialog(){ if(window.confirm("您确认要进行这个操作吗?")) alert("您选择了确定!"); else alert("您选择了取消"); } function promptDialog(){ var input=window.prompt("请输入内容:"); if(input!=null) { window.alert("您输入的内容为"+input); } } </script> </head> <body> <form> <input type="button" value="警告对话框" onclick="alertDialog()"> <input type="button" value="确认对话框" onclick="confirmDialog()"> <input type="button" value="输入对话框" onclick="promptDialog()"> </form> </body> </html> 请教:如何将JavaScript代码写到js文件中然后再用html代码调用呢? 展开
 我来答
回忆526
推荐于2016-05-11 · TA获得超过4165个赞
知道大有可为答主
回答量:3276
采纳率:87%
帮助的人:1860万
展开全部
将JavaScript代码从HTML文件中分离出来,我们只是需要将JS的代码都统一写在一个文件中,然后我们在通过window.onload的事件,这个就是html先加载完成,然后才会调用JS文件,这里还是通过代码来体现:
html中:<html>
<head></head>
<script type="text/javascript" src="waterfall1.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</html>
JS文件中:
window.onload = function(){
alert('我是JS文件代码');

}
百度网友3cd60cfbae6
推荐于2017-11-26 · TA获得超过126个赞
知道答主
回答量:126
采纳率:100%
帮助的人:123万
展开全部
function alertDialog(){ alert("您成功实行了这个操作"); } function confirmDialog(){ if(window.confirm("您确认要进行这个操作吗?")) alert("您选择了确定!"); else alert("您选择了取消"); } function promptDialog(){ var input=window.prompt("请输入内容:"); if(input!=null) { window.alert("您输入的内容为"+input); } } 将上面这些保存成“.js”文件,例如:temp.js,然后在要是用js的HTMl页面中加如下代码: <script type="text/javascript" src="temp.js"></script>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式