<!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript <h1> <p id = "demo"> JavaScript 能改变 HTML 元素的内容 </p> <script> function myFunction() { x = document.getElementById("demo"); //找到元素 x.innerHTML = "Hellp JavaScript!"; //改变内容 } </script> <p> <button type = "button" onclick = "myFunction()"> 点击这里 </button> </p> </body> </html>
打开网页后:
----------------------------------------------------
点击按钮后:
----------------------------------------------------