“JavaScript 输出”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“http://www.runoob.com/js/js-output.html”)
 
第1行: 第1行:
http://www.runoob.com/js/js-output.html
+
==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>
 +
  <body>
 +
 
 +
    <h1>我的第一个 Web 页面</h1>
 +
 
 +
    <p>我的第一个段落。</p>
 +
 
 +
    <button onclick="myFunction()">点我</button>
 +
 
 +
    <script>
 +
      function myFunction() {
 +
          document.write(Date());
 +
      }
 +
    </script>
 +
  </body>
 +
  </html>

2018年2月5日 (一) 00:31的版本

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>

</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>

</body>
</html> 

写到Html文档

  • 出于测试目的,您可以将JavaScript直接写在HTML 文档中:
  • 实例:
 <!DOCTYPE html>
 <html>
 <body>

我的第一个 Web 页面

我的第一个段落。

   <button onclick="myFunction()">点我</button>
   <script>
      function myFunction() {
         document.write(Date());
      }
    </script>
 </body>
 </html>