如何在HTML中嵌入JavaScript

 我来答
风中有龙
推荐于2018-02-27 · 超过11用户采纳过TA的回答
知道答主
回答量:48
采纳率:100%
帮助的人:15.5万
展开全部
javaScript 方法通过onclick事件触发

嵌入javaScript有两种书写方式:
1、内部js
1.1在script中编写

<script type="text/javascript">
function showOutter(){
// 通过id,获得输入内容
var content = document.getElementById("content").value;
alert(content);
}
</script>
</head>
<body>
<input type="text" id="content" />
<input type="button" value="弹出" onclick="showOutter();"/>
</body>

1.2直接写在标签中
<body>

<h1 onclick="this.innerHTML='hello!'">请点击这段文本!</h1>

</body>

2、外部js
只需在外部建立.js格式文档,将1.1<script>标签之间的代码写入.js文件中。然后
<head>
<script type="text/javascript" src='../**.js'></script>
</head>
<body>
<input type="text" id="content" />
<input type="button" value="弹出" onclick="showOutter();"/>
</body>
都知道吗
2016-04-28 · TA获得超过348个赞
知道小有建树答主
回答量:183
采纳率:100%
帮助的人:31.5万
展开全部
<html> <body> <script type="text/javascript"> document.write("Hello World!"); </script> </body> </html>

上面的代码会在 HTML 页面中产生这样的输出:Hello World!
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式