<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style type="text/css"> .message{ width:200px; height:100px; background-color:#CCC;} </style> </head> <body> <script type="text/javascript"> var element=document.createElement("p"); element.className="message"; var a=document.createTextNode("I love JavaScript!"); document.body.appendChild(element); element.innerHTML=a; </script> </body> </html>
你既然用了document.createTextNode(),就应该用element.appendChild()赋值,就错在element.innerHTML=a,改成:element.appendChild(a);就行了