JavaScript 可以通过不同的方式来输出数据:
1、window.alert() 弹出警告框
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>window.alert() 弹出警告框</title>
</head>
<body>
<h1>我的第一个页面</h1>
<p>我的第一个段落。</p>
<script>
window.alert("hello world");
</script>
</body>
</html>
————————————————————————————————
2、document.write() 方法将内容写到 HTML 文档中
示例1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>document.write()</title>
</head>
<body>
<h1>我的第一个Javascript 页面</h1>
<p>我的第一个段落。</p>
<script>
document.write(Date());
</script>
</body>
</html>
示例2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>document.write</title>
</head>
<body>
<h1>我的第一个javascript 页面</h1>
<p>我的第一个段落。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction()
{
document.write(Date());
}
</script>
</body>
</html>
————————————————————————————————
3、innerHTML 写入到 HTML 元素
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>innerHTML</title>
</head>
<body>
<h1>我的第一个javascript 页面</h1>
<p id="test">oooooooooo</p>
<p id="demo">我的第一个段落。</p>
<script>
document.getElementById("demo").innerHTML="段落已修改。";
</script>
</body>
</html>
————————————————————————————————
4、console.log() 写入到浏览器的控制台上
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>console.log</title>
</head>
<body>
<h1>我的第一个javascript 页面</h1>
<p>
浏览器中(Chrome, IE, Firefox) 使用 F12 来启用调试模式, 在调试窗口中点击 "Console" 菜单。
</p>
<script>
a = 5;
b = 6;
c = a + b;
console.log(c);
</script>
</body>
</html>