javascript中的打印方法有几种

文章2022-04-06122 人已阅来源:网络

javascript中的打印方法有三种,分别是window.alert()、document.write()和console.log()。

javascript中的打印方法有几种

本文操作环境:windows10系统、javascript 1.8.5、thinkpad t480电脑。

在JavaScript中,我们通常会使用以下三种方式来打印数据:

  • 使用 window.alert() 写入警告框

  • 使用 document.write() 写入 HTML 输出

  • 使用 console.log() 写入浏览器控制台

下面我们来看看他们是如何使用的。

使用 window.alert()

您能够使用警告框来显示数据:

实例

<!DOCTYPE html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<script>
window.alert(5 + 6);
</script>

</body>
</html>

使用 document.write()

出于测试目的,使用 document.write() 比较方便:

实例

<!DOCTYPE html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<script>
document.write(5 + 6);
</script>

</body>
</html>

使用 console.log()

在浏览器中,您可使用 console.log() 方法来显示数据。

请通过 F12 来激活浏览器控制台,并在菜单中选择“控制台”。

实例

<!DOCTYPE html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<script>
console.log(5 + 6);
</script>

</body>
</html>

相关视频教程分享:javascript视频教程

以上就是javascript中的打印方法有几种的详细内容!