.
3ss.cn

javascript中的打印办法有几种

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

在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>
赞(0)
未经允许不得转载:互联学术 » javascript中的打印办法有几种

评论 抢沙发