获取each()中索引的总长度
我想从表中获取第一个单元格和最后 3 个单元格之间的未知(变化)行数。我正在使用 jQuery 的each,但不知道为什么 $(this).length没有给出索引的总长度。
jQuery:
$("#parent table:first tr").each(function(i){
var goodlng = $(this).parent().children("tr").length -1; //this works
var badlng = $(this).length -1; //this doesn't! (always == -1)
});
goodlng
是好的实践吗?必须先去找父母,然后再回到孩子那里,这似乎是一种黑客行为。 这是一个 jsfiddle (已打开 console.log())。
HTML 示例:
<div id="parent">
<table>
<tr>
<td>unwanted 1</td>
</tr>
<tr>
<td>wanted!</td>
</tr>
<tr>
<td>unwanted2</td>
</tr>
</table>
</div>
tl;dr:为什么不 $(this).length == $(this).parent().children("tr").length
在each 函数内部。还有其他更好的方法吗?
I want to get an unknown (changing) # of rows from a table, between the 1st cell and the last 3. I'm using jQuery's each and don't know why $(this).length doesn't give the total length of the index.
jQuery:
$("#parent table:first tr").each(function(i){
var goodlng = $(this).parent().children("tr").length -1; //this works
var badlng = $(this).length -1; //this doesn't! (always == -1)
});
Is the goodlng
good practice? it seems like a hack to have to go to parent and then back to children.
Here is a jsfiddle (have console.log()) open.
Example HTML:
<div id="parent">
<table>
<tr>
<td>unwanted 1</td>
</tr>
<tr>
<td>wanted!</td>
</tr>
<tr>
<td>unwanted2</td>
</tr>
</table>
</div>
tl;dr: Why doesn't $(this).length == $(this).parent().children("tr").length
inside of an each function. and is there another better way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题说“单元格”,但您似乎正在尝试获取行数。
如果您确实希望将其放在
.each()
中,则可以使用siblings()
[docs] 方法和andSelf()
[docs] 方法。但如果行没有改变,为什么要重复呢?
或者,如果它们发生变化,我只需使用表上的本机
rows
属性来获取长度。Your question says "cells", but it seems like you're trying to get the number of rows.
If you really want it inside the
.each()
, you could use thesiblings()
[docs] method and theandSelf()
[docs] method.But if the rows aren't changing, why do it repetitively?
Or if they are changing, I'd just use the native
rows
property on the table to get the length.实际上,这个
$(this).parent().children("tr").length
是正确的处理方式。在上面的上下文中,this
是对tr
节点本身的引用。tr
.length =tr
的长度。另一方面, tr.parent().children("tr") 是节点兄弟节点的列表,因此 tr.parent().children("tr").length 是兄弟姐妹的数量。Actually, this
$(this).parent().children("tr").length
is the correct way of handling things.this
, in the context above is a reference to thetr
node itself.tr
.length = the length of thetr
.tr.parent().children("tr")
, on the other hand is a list of the node's siblings, sotr.parent().children("tr").length
is the number of siblings.这是每个元素的上下文,而不是全部元素。因此,
this
一次只能有一个tr
。这就是为什么长度不包含所有元素的原因。This is the context of EACH element, not all of them. Therefore,
this
is only ONEtr
at a time. That is why the length does not have all the elements.each() 函数不知道它正在处理的元素集合。它始终处理单个元素。这就是为什么当你去找家长询问所有孩子时,你会得到正确的答案。
仅指您正在处理的当前 tr 元素,而不是它们的集合。
The each() function is unaware of the collection of elements that it is working on. It is dealing with a single element at all times. Thats why when you go to the parent and ask for all children you get the right answer.
simply refers to the current tr element that you are dealing with, not a collection of them.