开发者社区 问答 正文

请问代码实现 outerHTML

请问代码实现 outerHTML

展开
收起
kun坤 2019-11-28 14:28:28 549 分享 版权
1 条回答
写回答
取消 提交回答
  • //说明:outerHTML其实就是innerHTML再加上本身;
    Object.prototype.outerHTML = function() {
      var innerCon = this.innerHTML, //获得里面的内容
        outerCon = this.appendChild(innerCon); //添加到里面
      alert(outerCon);
    };
    

    演示代码:

    
    <!--
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <title>Document</title>
      </head>
      <body>
        <div id="outer">
          hello
        </div>
        <script>
          Object.prototype.outerHTML = function() {
            var innerCon = this.innerHTML, //获得里面的内容
              outerCon = this.appendChild(innerCon); //添加到里面
            alert(outerCon);
          };
          function $(id) {
            return document.getElementById(id);
          }
          alert($("outer").innerHTML);
          alert($("outer").outerHTML);
        </script>
      </body>
    </html>
     -->
    

    微信截图_20191128143406.png

    2019-11-28 14:28:36
    赞同 展开评论
问答地址: