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

标签 javascript jquery html ajax jquery-filter

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

我只想返回 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");

*我还放了一个display: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');
    }
});

最佳答案

你实际上有这样的东西:

从您的调用返回的数据

  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

关于javascript - jquery 删除过滤响应中的特定 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33785008/

相关文章:

javascript - PHP 到 Javascript 将字符串与字符列表进行比较

javascript - 关闭 Chrome 中的弹出窗口似乎不会释放内存

javascript - jQuery .on 和 .trigger

javascript从在线链接获取html文件

javascript - 无法在导航栏中水平对齐菜单项

javascript - 使用 Greasemonkey 将文本更改为图像(在静态站点上)?

jquery - 透明可折叠与 jquerymobile

html - 在图像悬停时 - 添加一个占据图像宽度的标题

javascript - Twitter bootstrap 工具提示显示在模态后面

javascript - DIV 中的 OnClick 事件处理程序(第一个 OnClick 影响所有 DIV)