javascript中的打印方法有三种,分别是window.alert()、document.write()和console.log()。
本文操作环境: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中的打印方法有几种的详细内容,更多请关注其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。