第一个JavaScript程序
JavaScript的编写位置:
- 写在
.html文件中。 - 单独写在
.js文件中。
此例演示是写在 html 文件中
S1: 新建html文件 ( 如 MyFirstJavaScript.html)
S2: 在页面任意位置( 一般是body标签中 )编写js代码,创建标签script,在其中编写js代码。
<body>
<script>
// 此处编写javascript代码
</script>
</body>
S3: 编写具体代码,完成功能,打开刷新html文件时,弹出提示框。
<body>
<script>
// 此处编写javascript代码
alert("hello world !");
</script>
</body>
效果:
分析: 当打开( 刷新 )页面时,会弹出一个提示框。
完整的html页面代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 此处编写javascript代码
alert("hello world !");
</script>
</body>
</html>
JavaSCript注释:
1、多行注释
/* */
2、单行注释
//
通过注释可以简单的调试代码
注意事项和细节:
- js中区分
大小写 - js语句每条以分号结尾 (如果不写
分号也可以,但是会加大系统资源,浏览器会帮你加,但是有可能加错位置,加对了还好说,加错了,就不容易找到了) - 可以使用
多个空格和多行换行用于格式化结构,调整结构,便于读写