admin管理员组文章数量:1289866
I have a jQuery object that is created via jQuery .find()
as seen below...
var $mytable= $('#mytable');
var $myObject = $mytable.find("tbody tr");
This works great and creates a jQuery object of all the tr
elements in the tbody
. However, as I'm looping over the data, I need to be able to remove parts of the object as I go. For instance, if the above call returns a jQuery object named $myObject
with a length of 10, and I want to remove the index 10, I thought I could just do $myObject.splice(10,1)
and it would remove the element at index 10. However this doesn't seem to be working.
Any ideas why? Thank you!
UPDATE
I basically just want to be able to remove any element I want from $myObject as I loop through the data. I know it's zero based (bad example above I guess), was just trying to get my point across.
UPDATE
Okay, so I create the object using the find method on the table and at it's creation it's length is 24. As I loop over the object, when I hit an element I don't want I tried to use Array.prototype.splice.call($rows,x,1) where x represents the index to remove. Afterwards when I view the object in the console, it still has a length of 24.
I have a jQuery object that is created via jQuery .find()
as seen below...
var $mytable= $('#mytable');
var $myObject = $mytable.find("tbody tr");
This works great and creates a jQuery object of all the tr
elements in the tbody
. However, as I'm looping over the data, I need to be able to remove parts of the object as I go. For instance, if the above call returns a jQuery object named $myObject
with a length of 10, and I want to remove the index 10, I thought I could just do $myObject.splice(10,1)
and it would remove the element at index 10. However this doesn't seem to be working.
Any ideas why? Thank you!
UPDATE
I basically just want to be able to remove any element I want from $myObject as I loop through the data. I know it's zero based (bad example above I guess), was just trying to get my point across.
UPDATE
Okay, so I create the object using the find method on the table and at it's creation it's length is 24. As I loop over the object, when I hit an element I don't want I tried to use Array.prototype.splice.call($rows,x,1) where x represents the index to remove. Afterwards when I view the object in the console, it still has a length of 24.
Share Improve this question edited Dec 17, 2013 at 21:37 Phil asked Dec 17, 2013 at 21:21 PhilPhil 4,0699 gold badges70 silver badges117 bronze badges 4-
@A1rPun
$myObject.get().pop();
– A. Wolff Commented Dec 17, 2013 at 21:26 - @A.Wolff I soon realised :) But the answer depends on what the asker is doing with the output. – A1rPun Commented Dec 17, 2013 at 21:29
- It's difficult to give you a "best" answer since you're not telling us what you're doing this for. What do you want to do with this jQuery object after you've selected it? – Blazemonger Commented Dec 17, 2013 at 21:33
- @Blazemonger I want to be able to remove elements from the object and have it re-indexed so if it has a length of 24, and I remove the data at index 15, it will show a new length of 23 – Phil Commented Dec 17, 2013 at 21:38
6 Answers
Reset to default 5Use .not()
to remove a single element, then loop through the jQuery object at your leisure:
var $myObject = $mytable.find('tbody tr').not(':eq(9)'); // zero-based
http://jsfiddle/mblase75/tLP87/
http://api.jquery./not/
Or if you might be removing more than one:
var $myObject = $mytable.find("tbody tr:lt(9)");
http://jsfiddle/mblase75/9evT8/
http://api.jquery./lt-selector/
splice
is not part of the jQuery API, but you can apply native Array methods on jQuery collections by applying the prototype:
Array.prototype.splice.call($myObject, 9, 1); // 0-index
You can also use pop to remove the last item:
Array.prototype.pop.call($myObject);
This should also give you a correct length
property.
splice
is an array method, not a jQuery object method.
Try slice
Javascript uses zero-based arrays. This means that the final item in the array (i.e. the 10th item) will be at index 9.
$myObject[9]
So you need something like this:
$myObject.splice(9, 1);
This will remove the element from your existing array, and also return it.
You could also use filter :
var $myObject = $mytable.find("tbody tr").filter(':lt(9)');
You can use .remove to remove an element from the DOM.
So to remove the element at index 9 of the $myObject
array, use:
$myObject.eq(9).remove();
If you want to keep the element that you are removing, you can also do:
var removedElement = $myObject.eq(9);
removedElement.detach();
本文标签: javascriptRemove element from jQuery objectStack Overflow
版权声明:本文标题:javascript - Remove element from jQuery object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741440778a2378904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论