您好,欢迎访问一九零五行业门户网

javascript怎么打印

在javascript是不提供任何内建的打印或显示函数,但能够以不同方式“显示”数据,分别是:1、“window.alert()”;2、“document.write()”;3、“innerhtml”;4、“console.log()”。
本文操作环境:windows7系统、javascript1.8.5版,dell g3电脑
javascript怎么打印?
javascript 不提供任何内建的打印或显示函数。
javascript 显示方案
javascript 能够以不同方式“显示”数据:
使用 window.alert() 写入警告框
使用 document.write() 写入 html 输出
使用 innerhtml 写入 html 元素
使用 console.log() 写入浏览器控制台
使用 innerhtml
如需访问 html 元素,javascript 可使用 document.getelementbyid(id) 方法。
id 属性定义 html 元素。innerhtml 属性定义 html 内容:
实例
<!doctype html><html><body><h1>我的第一张网页</h1><p>我的第一个段落</p><p id="demo"></p><script> document.getelementbyid("demo").innerhtml = 5 + 6;</script></body></html>
提示:更改 html 元素的 innerhtml 属性是在 html 中显示数据的常用方法。
使用 document.write()
出于测试目的,使用 document.write() 比较方便:
实例
<!doctype html><html><body><h1>我的第一张网页</h1><p>我的第一个段落</p><script>document.write(5 + 6);</script></body></html>
注意:在 html 文档完全加载后使用 document.write() 将删除所有已有的 html :
实例
<!doctype html><html><body><h1>我的第一张网页</h1><p>我的第一个段落</p><button onclick="document.write(5 + 6)">试一试</button></body></html>
提示:document.write() 方法仅用于测试。
使用 window.alert()
您能够使用警告框来显示数据:
实例
<!doctype html><html><body><h1>我的第一张网页</h1><p>我的第一个段落</p><script>window.alert(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怎么打印的详细内容。
其它类似信息

推荐信息