js清除节点内容(改变标签元素)

<!DOCTYPE HTML>
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>js清除节点内容||改变标签元素</title>
    </head>

    <body>
        <!--清除节点内容-->
        <div id="content">
            <h1>html</h1>
            <h1>php</h1>
            <h1>javascript</h1>
            <h1>jquery</h1>
            <h1>java</h1>
        </div>
        <button onclick="clearText()">清除节点内容</button>
        <!--改变标签元素-->

        <div>
            <b id="oldnode">JavaScript</b> 是一个很常用的技术,为网页添加动态效果。
        </div>
        <a href="javascript:replaceMessage()"> 将加粗改为斜体</a>
    </body>
    <script type="text/javascript">
        //     清除节点元素
        function clearText() {
            var content = document.getElementById("content");
            // 在此完成该函数
            var x = content.removeChild(content.childNodes[1]);

        }
        //     改变标签元素
        function replaceMessage() {
            var oldnode = document.getElementById("oldnode");
            var newnode = document.createElement("i");
            newnode.innerHTML = oldnode.innerHTML;
            oldnode.parentNode.replaceChild(newnode, oldnode);

        }
    </script>

</html>

原文地址:https://www.cnblogs.com/shoolnight/p/6782195.html