JavaScript 可以通过不同的方式“显示”数据:
使用 innerHTML
写入 HTML 元素。
使用 document.write()
写入 HTML 输出。
使用 window.alert()
写入警报框。
使用 console.log()
写入浏览器控制台。
innerHTML
要访问 HTML 元素,JavaScript 可以使用 document.getElementById(id)
方法。
id
属性定义 HTML 元素。 innerHTML
属性定义 HTML 内容:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My First Paragraph.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
更改 HTML 元素的innerHTML 属性 是在 HTML 中显示数据的常用方法。
出于测试目的,使用 document.write()
很方便:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<p>Never call document.write after the document has finished loading.
It will overwrite the whole document.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
加载 HTML 文档后使用 document.write() 将删除所有现有 HTML:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<button type="button" onclick="document.write(5 + 6)">Try it</button>
</body>
</html>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<button type="button" onclick="document.write(5 + 6)">Try it</button>
</body>
</html>
document.write() 方法只能用于测试。
您可以使用警报框来显示数据:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
您可以跳过 window
关键字。
在 JavaScript 中,window 对象是全局范围对象。这意味着变量、属性和方法默认属于 window 对象。 这也意味着指定 window
关键字是可选的:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
alert(5 + 6);
</script>
</body>
</html>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<script>
alert(5 + 6);
</script>
</body>
</html>
出于调试目的,您可以调用 console.log()
方法 在浏览器中显示数据。
您将在后面的章节中了解有关调试的更多信息。
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>Activate Debugging</h2>
<p>F12 on your keyboard will activate debugging.</p>
<p>Then select "Console" in the debugger menu.</p>
<p>Then click Run again.</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript 没有任何打印对象或打印方法。
您无法从 JavaScript 访问输出设备。
唯一的例外是您可以调用 window.print()
方法 浏览器打印当前窗口的内容。
<!DOCTYPE html>
<html>
<body>
<button onclick="window.print()">Print this page</button>
</body>
</html>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>The window.print() Method</h2>
<p>Click the button to print the current page.</p>
<button onclick="window.print()">Print this page</button>
</body>
</html>