我正在进行 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());
关于javascript - jquery 删除过滤响应中的特定 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33785008/