[英]jQuery .remove() with selector doesn't work
According to the .remove() | 根据.remove()| jQuery API Documentation , it is perfectly valid to include a selector as an optional parameter to .remove()
. 在jQuery API文档中 ,将选择器作为.remove()
的可选参数包含在内是完全有效的。 Quote: 引用:
We can also include a selector as an optional parameter. 我们还可以包含一个选择器作为可选参数。 For example, we could rewrite the previous DOM removal code as follows: 例如,我们可以重写以前的DOM删除代码,如下所示:
$( "div" ).remove( ".hello" );
So I've written 2 divs to test this out: 所以我写了两个div来测试这个:
<div id="div1">test
<div id="div2">Remove</div>
</div>
Using this as jQuery: 使用它作为jQuery:
$( document ).ready(function() {
$( "#div1" ).remove( "#div2" );
});
It didn't remove the div as expected. 它没有按预期删除div。 The result was: 结果是:
test 测试
Remove 去掉
Instead using: 而是使用:
$( document ).ready(function() {
$("#div2").remove();
});
Removes the div as expected. 按预期删除div。 So what am I missing here? 那我在这里错过了什么? Is the documentation wrong? 文档错了吗? Did I misunderstood something? 我误解了什么吗?
You're misunderstanding what the selector parameter is doing. 你误解了选择器参数的作用。 It is filtering the first set of objects. 它正在过滤第一组对象。
From the docs : 来自文档 :
A selector expression that filters the set of matched elements to be removed. 一个选择器表达式,用于过滤要删除的匹配元素集。
So, your "#div2"
selector doesn't exist as a part of the "#div1"
element. 因此, "#div2"
选择器不作为"#div1"
元素的一部分存在。 For example, say I have the following: 例如,假设我有以下内容:
<div class="red">Red</div>
<div class="red">Red</div>
<div class="red">Red</div>
<div class="red notneeded">Red</div>
<div class="red notneeded">Red</div>
<div class="red">Red</div>
Then, I call the following: 然后,我打电话给以下人员:
$(function () {
$("div.red").remove(".notneeded");
});
I would be left with the following: 我将留下以下内容:
<div class="red">Red</div>
<div class="red">Red</div>
<div class="red">Red</div>
<div class="red">Red</div>
So, the jQuery matched set is all divs with a class of red
- the second selector ( ".notneeded"
) will filter that first matched set by the ones with a class of notneeded
- then it will remove them. 因此,jQuery匹配集是所有具有red
类的div - 第二个选择器( ".notneeded"
)将过滤那些具有notneeded
的类的第一个匹配集 - 然后它将删除它们。
The reason your code doesn't work is that the selector is used to filter the original list ( "#div1"
in your case). 您的代码不起作用的原因是选择器用于过滤原始列表(在您的情况下为"#div1"
)。 You can't remove children like this. 你无法删除这样的孩子。 You have to select the children then .remove()
instead: 你必须选择孩子然后.remove()
代替:
$('#div1').children('#div2').remove() // or .find('#div2').remove() if it's nested deeper
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div1">test <div id="div2">Remove</div> </div>
The place where the selector does work is when you actually filter the list, like here: 选择器工作的位置是您实际过滤列表时,如下所示:
$('div').remove('.remove')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div1" class="keep">test1</div> <div id="div2" class="remove">test2</div> <div id="div3" class="keep">test3</div>
This is the code that is not working right? 这是代码不能正常工作?
$( document ).ready(function() {
$( "#div1" ).remove( "#div2" );
}); });
And this is what you're trying to do? 这就是你要做的事情?
$( "div" ).remove( ".hello" );
Try this. 尝试这个。 The code above means that it will remove all "div" tag that contains a class = "hello". 上面的代码意味着它将删除包含class =“hello”的所有“div”标记。
$( document ).ready(function() {
$( "div" ).remove( "#div2" );
}); });
A example of how .remove( [selector ] ) works: .remove([selector])如何工作的一个例子:
$('.of-these-items').remove('.these-items');
if we have: 如果我们有:
<ul>
<li class="of-these-items">1</li>
<li class="of-these-items">2</li>
<li class="of-these-items these-items">3</li>
<li class="of-these-items these-items">4</li>
<li class="of-these-items">5</li>
<ul>
" of these items , remove these items ": “ 这些项目 ,删除这些项目 ”:
<ul>
<li class="of-these-items">1</li>
<li class="of-these-items">2</li>
<li class="of-these-items">5</li>
<ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.