zoukankan      html  css  js  c++  java
  • JS创建替换删除节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    <script type="text/javascript">
        function CreateNode() {
            var pnode = document.createElement('p');
            var tnode = document.createTextNode('烟花三月下扬州');
            pnode.appendChild(tnode);
            document.body.appendChild(pnode);
        }
        function ReplaceNode() {
            var pnode = document.createElement('p');
            var tnode = document.createTextNode('故人西辞黄鹤楼');
            pnode.appendChild(tnode);
            //获取要替换的p节点
            var oldnode = document.getElementsByTagName('p')[0];
            //oldnode.replaceNode(pnode,oldnode);//此种方法只支持IE
            oldnode.parentNode.replaceChild(pnode, oldnode); //通用的
        }
        function RemoveNode() {
            var oldnode = document.getElementsByTagName('p')[0];
            //oldnode.parentNode返回的是p节点的父节点,这里就是body,然后使用body节点的removeChild方法删除其下面的pnode节点
            oldnode.parentNode.removeChild(oldnode);
        }
        </script>
    </head>
    <body>
        <input id="Button1" type="button" value="创建节点" onclick="CreateNode();" /><br />
        <input id="Button2" type="button" value="替换节点" onclick="ReplaceNode();" /><br />
        <input id="Button3" type="button" value="删除节点" onclick="RemoveNode();" />
    </body>


    </html>

  • 相关阅读:
    poj 1579(动态规划初探之记忆化搜索)
    hdu 1133(卡特兰数变形)
    CodeForces 625A Guest From the Past
    CodeForces 625D Finals in arithmetic
    CDOJ 1268 Open the lightings
    HDU 4008 Parent and son
    HDU 4044 GeoDefense
    HDU 4169 UVALive 5741 Wealthy Family
    HDU 3452 Bonsai
    HDU 3586 Information Disturbing
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/3024361.html
Copyright © 2011-2022 走看看