获取document.getElementById()时,使用innerHTML获取标签获取内容,要注意:
1.要让内容先加载完,才可以使用,不然获取的是空,使用:window.onload = ....
2.或者将script放在内容的后面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function() { var myID = document.getElementById("con"); // alert("test"); document.write("原来html的内容:" + myID.innerHTML + "<br/>"); myID.innerHTML = "change html"; document.write("现在html的内容:" + myID.innerHTML); } </script> </head> <body> <p id="con">hello world</p> </body> </html>