简体   繁体   English

如何从 jquery 对象集合中删除 jquery 对象?

[英]How do you remove a jquery object from a jquery object collection?

I realize that this is somewhat of a duplicate question, I've even seen jQuery remove object from object collection question, but I'm hoping that there is a better answer that is more complete.我意识到这是一个有点重复的问题,我什至看到jQuery remove object from object collection问题,但我希望有一个更完整的更好的答案。

If I were to create a collection, such as $divs = $( 'div' );如果我要创建一个集合,例如$divs = $( 'div' ); it could return several divs in a collection, but if I needed to remove/delete one of them from the collection, not from the DOM, I don't see a jQuery command to do this.它可以返回一个集合中的多个 div,但是如果我需要从集合中而不是从 DOM 中删除/删除其中一个,我看不到执行此操作的 jQuery 命令。 One of the suggested answers in the linked article solution says that a null is left in the collection, which I'd rather not have happen.链接文章解决方案中的建议答案之一说集合中保留了一个空值,我宁愿不要发生这种情况。

In my case, all of the objects in my collection 'look' the same when the collection is created, that is there are no class, attributes, or attribute value differences that I can use in a CSS Selector to exclude the object item that I want to remove from my collection.就我而言,我的集合中的所有对象在创建集合时都“看起来”相同,也就是说,没有可以在 CSS 选择器中使用的类、属性或属性值差异来排除我想从我的收藏中删除。 What happens is that a user clicks a button next to the item to be removed, and from that click, I determine what item needs to be removed.发生的情况是,用户单击要删除的项目旁边的按钮,然后通过单击确定需要删除的项目。

I could use the button click to temporarily modify the div.drop-down-list, and then use that difference to exclude the one element when I create the jQuery collection, but I was hoping for something cleaner.我可以使用按钮单击来临时修改 div.drop-down-list,然后在创建 jQuery 集合时使用该差异来排除一个元素,但我希望有更清晰的东西。

Except for the code that handles the button click and determining which div.drop-down-list to remove, here is the code I'm trying to create, except that there isn't a removeObject statement in jQuery.除了处理按钮单击并确定要删除哪个 div.drop-down-list 的代码之外,这里是我正在尝试创建的代码,只是在 jQuery 中没有 removeObject 语句。

$div = $( 'div.drop-down-list' ); // Get all of the drop-down-lists
$div.removeObject( $( clickedDropDownList ) ); // Create a new jQuery object for the one to exclude.
$div.hide();  // Hide/Close all the others.

It seems strange that new jQuery object items can easily be added to a collection using the add( ... ) statement, but there isn't an easy way to remove them.使用add( ... )语句可以轻松地将新的 jQuery 对象项添加到集合中,这似乎很奇怪,但是没有一种简单的方法可以删除它们。

Well I think what you want is to filter the array not to remove the target.好吧,我认为您想要的是过滤数组而不是删除目标。 You can try achieve this using filter method like below:您可以尝试使用如下filter方法实现此目的:


$div = $( 'div.drop-down-list' ); // Get all of the drop-down-lists
cosnt $filtered = $div.filter((ix,el)=> el !== clickedDropDownList  )
$filtered.hide(); 
// assumed that clickedDropDownList is a native element not a jQuery object.

// later on if you want to do something with all the items use the `$div` 
// if you want the previously filtered items use `$filtered`
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM