jquery 删除过滤后的响应中的特定 div

jquery remove specific div within filtered response

我正在进行 GET ajax 调用,它正在 returning html 作为数据。

我只想 return div#product-detail-response 中的内容并将 html 替换为 div.product-detail-info 中的内容.

效果很好,但是当我以管理员身份登录 CMS 时,它会在每一页的末尾吐出调试代码。我知道我可以禁用该功能,但我想保留它并从响应中删除调试 div。

*div#debugging-info 存在于 div#product-detail-response

// this displays the html correctly, but it includes the debugging-info that I want removed
var $response=$(data);
$(".product-detail-info").html($response.filter('#product-detail-response').html());

// I tried adding a remove in different spots of this line without success
$(".product-detail-info").html($response.filter('#product-detail-response').html()).remove("#debugging-info");

*我也放了一个显示:none;对于 .product-detail-info #debugging-info {} 和这个:

$("#debugging-info").hide();

在上面的代码之后。

回复:

<div class="product-detail-info">
   html and all the stuff I want to show up
   <div id="debugging-info">
      this shows debugging info, and I want to remove this div completely
   </div>
</div>

我想要的回复:

<div class="product-detail-info">
   html and all the stuff I want to show up
</div>

AJAX 通话

$.ajax({
    url: '/product_detail_ajax/'+entry_id,
    type: 'GET',
    data : {},
    dataType: 'html',
    context: this,
    success: function (data) {
        var $response=$(data);
        $(".product-detail-info").html($response.filter('#product-detail-response').html());
        $(".breadcrumb-product-title").text($response.filter('#breadcrumb-product-title').text());
    },
    error: function (data) {
        // console.log('error');
    }
});

dom 中注入响应 html 后,您可以隐藏调试信息元素,如下所示:

$response = $( data );
$( ".product-detail-info" ).html( $response.html() ).find( "#debugging-info" ).hide();

这是一个JS Fiddle

你实际上有这样的东西:

您的通话返回的数据

  var data = "<div class='product-detail-info'>html and all the stuff I want to show up<div id='debugging-info'>this shows debugging info, and I want to remove this div completely</div></div>";

现在创建一个 dom obj 来操作您的数据

  var parsed = $('<div/>').html(data);

  parsed.find("#debugging-info").remove();

  $("#result").append(parsed.html());

FIDDLE