“JavaScript 输出”的版本间的差异
来自CloudWiki
(未显示同一用户的1个中间版本) | |||
第47行: | 第47行: | ||
<nowiki><!DOCTYPE html> | <nowiki><!DOCTYPE html> | ||
<html> | <html> | ||
+ | <head> | ||
+ | <script> | ||
+ | function myFunction() { | ||
+ | document.write(Date()); | ||
+ | } | ||
+ | </script> | ||
+ | </head> | ||
<body> | <body> | ||
第55行: | 第62行: | ||
<button onclick="myFunction()">点我</button> | <button onclick="myFunction()">点我</button> | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
</body> | </body> | ||
</html></nowiki> | </html></nowiki> | ||
+ | |||
+ | *下一节 [[JavaScript If...Else 语句]] |
2018年2月5日 (一) 00:46的最新版本
Javascript显示数据
- JavaScript 可以通过不同的方式来输出数据:
使用 window.alert() 弹出警告框。 使用 document.write() 方法将内容写到 HTML 文档中。 使用 innerHTML 写入到 HTML 元素。
使用 window.alert()
- 你可以弹出警告框来显示数据:
- 实例:
<!DOCTYPE html> <html> <head> <script> function myFunction(){ alert("段落已修改。"); } </script> </head> <body> <button onclick="myFunction()">点我</button> <p id="demo" ></p> </body> </html>
操控HTML 元素
- 如需从 JavaScript 访问某个 HTML 元素,您可以使用 document.getElementById(id) 方法。
- 请使用 "id" 属性来标识 HTML 元素,并 innerHTML 来获取或插入元素内容:
- 实例:
<!DOCTYPE html> <html> <head> <script> function myFunction(){ document.getElementById("demo").innerHTML = "段落已修改。"; } </script> </head> <body> <button onclick="myFunction()">点我</button> <p id="demo" ></p> </body> </html>
写到Html文档
- 出于测试目的,您可以将JavaScript直接写在HTML 文档中:
- 实例:
<!DOCTYPE html> <html> <head> <script> function myFunction() { document.write(Date()); } </script> </head> <body> <h1>我的第一个 Web 页面</h1> <p>我的第一个段落。</p> <button onclick="myFunction()">点我</button> </body> </html>