3
刪除元素讓我們說,我想通過Ajax加載該文件:從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
}
});