createElement() 方法通过指定名称创建一个元素

createTextNode() 可创建文本节点

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript">
            window.onload=function(){
                //根据id获得对象
                var firstdiv=document.getElementById("firstdiv");
                //创建元素对象
                var h1=document.createElement("h1");
                //创建文本对象
                var content=document.createTextNode("Hello World!");
                //添加对象到相应的节点
                h1.appendChild(content);
                firstdiv.appendChild(h1);
            }
        </script>
    </head>
    <body>
        <div id="firstdiv"></div>
    </body>
</html>

 

相关文章:

  • 2021-12-29
  • 2021-11-06
  • 2022-12-23
  • 2021-07-31
  • 2021-12-30
  • 2022-12-23
猜你喜欢
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2021-04-03
  • 2021-12-24
  • 2021-06-17
  • 2022-02-06
相关资源
相似解决方案