第 1 种:弹出对话框模式
通过 alert() 函数弹出对话框,这是最常用的方式。在 script 标签下,调用 window.alert() 函数,括号内填入输出的内容。
<!DOCTYPE html>
<html>
<body>
<script>
window.alert("Hello,World!");
</script>
</body>
</html>
第 2 种:控制台模式
使用控制台输出通过 console 输出到控制台,在 script 标签下,调用 console.log() 函数,括号内填入输出的内容。
<!DOCTYPE html>
<html>
<body>
<script>
console.log("Hello,World!");
</script>
</body>
</html>
除此之外,还有 console.error()、console.warn() 等方式实现输出,分别表示输出错误,输出警告。
第 3 种:页面输出模式
通过 document.write() 实现输出到页面。在 document.write() 函数的括号中填入输出的内容即可。例子如下:
<!DOCTYPE html>
<html>
<body>
<script>
document.write("Hello,World!");
</script>
</body>
</html>