开发者

jQuery Selectors: Grabbing a section of children that are of a given tag

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 posi开发者_StackOverflow社区tion (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜