DOM基础教程之使用DOM(4)


<head>
        <title>childNodes</title>
        <meta charset="utf-8" />
        <script language="javascript">
            window.onload = function() {
                var bkk = document.getElementById("new5");
                var clickbk = document.getElementById("qiehuan");
                clickbk.onclick = dsqiehuan;
                function dsqiehuan() {
                    bkk.setAttribute("class", "xxx")
                }
            }
        </script>
        <style>
            .xxx{color:#ddd}
        </style>
    </head>
    <body>
        <div>
            555
        </div>
        <em>切换</em>
    </body>

8.createElement() 创建节点

复制代码 代码如下:


    <head>
        <title>childNodes</title>
        <meta charset="utf-8" />
        <script language="javascript">
            window.onload = function() {
        var oP = document.createElement("p");
        var oText = document.createTextNode("使用dom创建节点");
        var oText1 = document.createTextNode("使用dom创建节点123");
        oP.appendChild(oText);
        oP.appendChild(oText1);
        document.body.appendChild(oP);
            }
        </script>
    </head>
    <body>
    <p>这里本来有个P,测试createElement()</p>
    </body>

9.removeChild移除节点

复制代码 代码如下:


<head>
        <title>childNodes</title>
        <meta charset="utf-8" />
        <script language="javascript">
            window.onload = function() {
        var oP = document.getElementsByTagName("p")[0];
        oP.parentNode.removeChild(oP);//结尾为 .removeChild(oP),不是.removeChild("oP")
            }
        </script>
    </head>
    <body>
    <p>这里本来有个P,测试createElement()</p>
    </body>

10.insertBefore() 在特定节点前插入节点

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxypd.html