zoukankan      html  css  js  c++  java
  • DOM节点-appendChild

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <ul id="ele1">
            <li>11111111111111111111</li>
            <li id="li2">22222222222222222222</li>
            <li>33333333333333333333</li>
            <li>4444444444444444444</li>
            <li>5555555555555555555</li>
        </ul>
    </body>
        <script type="text/ecmascript">
            //给元素添加节点
            var ele = document.getElementById('ele1');
            //创建li元素和文本内容
            var oLi = document.createElement('li');
            var oText = document.createTextNode('今天由我主持晨会呵呵');
            //appendChild将子节点加到当前节点的后面
            oLi.appendChild(oText);
            //定义一个新节点和一个老节点
            function inserAfter(newEle, oldEle) {
                var next = oldEle.nextSibling;
                //判断节点后面是否有弟弟节点
              
                if (next) {
                    //如果有弟弟节点
                    next.parentNode.insertBefore(newEle, next);
                } else {
                    //如果没有弟弟节点
                    oldEle.parentNode.appendChild(newEle);
                }

            }
            var oLi2 = document.getElementById("li2");
            inserAfter(oLi, oLi2);

        </script>
    </html>

  • 相关阅读:
    navBar
    strong ,weak
    Linux基础-07-系统的初始化和服务
    Linux基础-06-vi编辑器
    Linux基础-05-正文处理、tar、解压缩
    Linux基础-04-权限
    Linux基础-03-用户、群组
    Linux基础-02-目录文件管理
    Linux基础-01-Linux基础命令
    oh my zsh 安装
  • 原文地址:https://www.cnblogs.com/xiaoleidiv/p/3290761.html
Copyright © 2011-2022 走看看