javascript - Remove a div from .get() request
问题描述
How do I remove
a div (lets name it div_2
) from div_1
which is been loaded via .get() with this code:
$.get('mysite.html', {}, function(data) {
var $response = $('<div />').html(data);
var $div_1 = $response.find('#div_1');
$('#container-div').append($div_1);
},'html');
I need it to be done only with this code, so no .load() and etc.
I tried this:
$.get('mysite.html', {}, function(data) {
var $response = $('<div />').html(data);
var $div_1 = $response.find('#div_1');
$('#container-div').append($div_1, function() {
$('#div_2').remove();
});
},'html');
Didn't work. Can someone help me with this?
解决方案
You've parsed the data and stored the parsed result in $response
. Just as you've found div_1
in it with find
, you can do the same to find div_2
in it — and then remove
it. Either initially after parsing, or after finding #div_1
:
$.get('mysite.html', {}, function(data) {
var $response = $('<div />').html(data);
var $div_1 = $response.find('#div_1');
$div_1.find("#div_2").remove(); // <====================
$('#container-div').append($div_1);
},'html');
推荐阅读
- javascript - 如何将数组推送到对象中不存在的对象?
- entity-framework-core - EntityFramework 核心链接表(视图)和导航属性
- kotlin - 为什么不在 Kotlin 中为 NULL 对象实现单例模式?
- python - 何时以及为什么使用 self.__dict__ 而不是 self.variable
- excel - 迭代计算以找到所需的数字
- python - 在约 50% 的情况下,Python docker 容器在绑定挂载后获取空文件
- types - Rest Client 中的数据类型
- python - 安装 3.7 时,Tensorflow 更新强制 Python3.5
- javascript - 如何组合“onchange”和“blur”HTML DOM 事件
- google-apps-script - 根据列号数组移动列