原型应用, HTMLElement ,给所有HTML元素都添加一个新方法

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

    <!-- <div> -->
        <div id="a">aaaaaa</div>
        <div id="b">bbbbbb</div>

    <!-- </div> -->
    




    <script type="text/javascript">

        HTMLElement.prototype.remove = function(){
            if (this.parentNode){
                this.parentNode.removeChild(this);
            };
        };

        
            var a = document.getElementById("a");
            a.parentNode.removeChild(a);

            // 
            document.getElementById("b").remove();
    </script>
</body>
</html>

 

posted @ 2022-06-13 23:35  AngDH  阅读(86)  评论(0编辑  收藏  举报