通过EQ过滤删除元素不会从jQuery Object中删除元素 [英] Removing element by EQ filtering doesn't remove the element from jQuery Object
问题描述
我使用以下代码来匹配元素列表:
I used this code to match list of elements:
var previewItems = preview.find('.items').children().not('.heads');
一切正常,但是当我尝试通过选择EQ来删除其中一项时,将其从DOM中删除后,将其保留在匹配元素的jQuery对象中.
Everything works fine, but when I try to remove one of the items by choosing it's EQ, after it removes from the DOM, it keeps in the jQuery Object of matched elements.
previewItems.eq(index).remove();
alert(previewItems.eq(index)); // [object Object]
为什么jQuery将元素保留在匹配元素的组中?
Why the jQuery keeps the element in the group of the matched elements?
推荐答案
为什么jQuery将元素保留在匹配元素的组中?
Why the jQuery keeps the element in the group of the matched elements?
如果要在删除后对其进行处理. remove
从DOM中删除该元素,但是您可能希望做进一步的事情(例如将其添加到DOM中的其他位置或从中检索信息).
In case you want to do something with it after removing it. remove
removes the element from the DOM, but you may well want to do something further (like adding it elsewhere in the DOM or retrieving information from it).
您可以使用 slice
或 filter
(或更准确地说:您可以获得具有所有相同元素的 new 对象,但slice
/not
省略的元素除外):
You can remove it from the set using slice
or not
or filter
(or more accurately: You can get a new object with all the same elements except the ones omitted by slice
/not
):
previewItems.eq(index).remove();
previewItems = previewItems.not(previewItems[index]);
这篇关于通过EQ过滤删除元素不会从jQuery Object中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!