W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <p>单击该按钮可创建包含 H1 和 P 元素的 SECTION 元素。</p> <button onclick="myFunction()">试一试</button> <script> function myFunction() { var x = document.createElement("SECTION"); x.setAttribute("id", "mySection"); document.body.appendChild(x); var heading = document.createElement("H1"); var txt1 = document.createTextNode("Section Heading"); heading.appendChild(txt1); document.getElementById("mySection").appendChild(heading); var para = document.createElement("P"); var txt2 = document.createTextNode("Some text in section.."); para.appendChild(txt2); document.getElementById("mySection").appendChild(para); } </script> </body> </html>