【问题标题】:Get HTML content by outerHTML and remove one div通过outerHTML获取HTML内容并去掉一个div
【发布时间】:2017-04-17 03:16:23
【问题描述】:

我正在使用 outerHTML 获取 HTML 标记的内容

var t=$("html")[0].outerHTML;

但我需要从结果中删除特定的 div

<div id="admin_panel">...</div>

这是我的 jQuery 代码:

$(function() {
    $('#save').click(function(){
        var t=$("html")[0].outerHTML;
        $.ajax({
            type: "POST",
            url: "save.php",
            data: { code: t },
            cache: false,
            success: function(html){
                alert('Changed saved');
            }
        });
    })
});

如何正确删除 div?

【问题讨论】:

    标签: jquery html outerhtml


    【解决方案1】:

    无损:

    jQuery: outer html()

    var $div = $('<div/>').append($('html').clone()).html();
    $div.find("#admin-panel").remove();
    var t=$div.html();
    

    【讨论】:

      【解决方案2】:

      看起来像这样:

      $(function() {
          $('#save').click(function(){
              var t = $("html")[0].outerHTML;
              var $t = $(t);
              $t.find("#admin_panel").remove();
              t = $t[0].outerHTML;
              $.ajax({
                  type: "POST",
                  url: "save.php",
                  data: { code: t },
                  cache: false,
                  success: function(html){
                      alert('Changed saved');
                  }
              });
          })
      });
      

      【讨论】:

      • 哎呀!固定答案。试试这个
      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多