从Ajax Response中删除元素

时间:2011-11-28 01:50:09

标签: jquery ajax wrapper

我想说我想通过AJAX加载这个文件:

<!-- loadme.html -->

<div class='content'>
    Hello !
    <div class='removeme'>Remove me, please.</div>
</div>

我如何才能获得Hello内容?我尝试了多种方法来移除.removeme div,它总是失败:

$.ajax({
    url: 'loadme.html',
    success: function(data) {
        var response = $('<div />').html(data);

        // First try :
        var content1 = response.find('.content').html()
        console.log(content1); // Return : Hello ! <div class="removeme">Remove me, please.</div>

        // Second Try :
        var content2 = response.find('.content').remove('.removeme').html()
        console.log(content2); // Return : Hello ! <div class="removeme">Remove me, please.</div>

        // Third Try :
        var content3 = response.find('.content').html();
        console.log($(content3).remove('.removeme').html()); // Return : Remove me, please
    }
});

1 个答案:

答案 0 :(得分:8)

尝试:

var temp = response.find('.content');
temp.children('.removeme').remove();
var content4 = temp.html();