书写位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
alert("这是一个弹窗");
</script>
</head>
<body>
</body>
</html>
- 内嵌式
<script>
alert("这是一个弹窗");
</script>
- 外联式
<script src = "demo.js"></script>
script只准有一种格式。
下面这种是不允许的
<script src = "demo.js">
alert("");
</script>
三种输出格式
- 弹窗输出
<script>
alert("这是一个弹窗");
</script>
- 控制台输出(一般情况下用于代码调试)
<script>
console.log("控制台输出,网页中不显示");
</script>
- 页面输出
<script>
document.write("这是一句话");
</script>
- 如果内容中含有标签会自动解析
<script>
document.write("<h1>这是一句话</h1>");
</script>
- 若想在当前页面输出字符,需使用转义字符
< >
替换<>
<script>
document.write("<h1>这是一句话</h1>");
</script>
注释
- 单行注释
ctrl + / - 多行注释
ctrl + shift + /