jQuery选择器:抓取一段给定标签的子节点 [英] jQuery Selectors: Grabbing a section of children that are of a given tag
问题描述
请注意,我可以处理数以千计的孩子,所以我不想把数组切片在数千个例行的基础上。
编辑:没有必要经过jQuery,如果有一个更有效的选项直接到DOM ...
您可以使用 .slice()
,例如:
$(tr td)。slice(2,7)
//如果你有< tr>
$(this).children(td)。slice(2,7)
以上将获得第3到第7个< td>
,因为它是一个基于0的 索引。或者是没有jQuery的版本,比方说你有< tr>
DOM元素:
code> var tds = tr.getElementsByTagName(td);
for(var i = 2; i< 7; i ++){
// do something
}
Say I have an arbitrary number children (e.g. "td"s) of a given element (e.g. a "tr"), and I need to grab a given number of these (say, 4) at a given position (say, 3; td's 3 - 67, in this case). What would the best query for doing this look like?
Note that I could be dealing with a potentially thousands of children, so I'd like to not be slicing up arrays in the thousands on a routine basis.
Edit: It doesn't have to go through jQuery, if there's a more efficient option that goes straight to the DOM...
You can use .slice()
for this, for example:
$("tr td").slice(2, 7)
//of if you have the <tr>
$(this).children("td").slice(2, 7)
The above would get the 3rd through 7th <td>
, since it's a 0-based index. Or the jQuery-less version, say you have the <tr>
DOM element:
var tds = tr.getElementsByTagName("td");
for(var i = 2; i<7; i++) {
//do something
}
You can test both versions here.
这篇关于jQuery选择器:抓取一段给定标签的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!