jQuery选择器:抓取一段给定标签的子节点 [英] jQuery Selectors: Grabbing a section of children that are of a given tag

查看:118
本文介绍了jQuery选择器:抓取一段给定标签的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个任意数字的孩子(例如td)一个给定的元素(例如tr),我需要在给定的位置抓住给定数量的这些(例如4) ,3; td的3 - 6 7 ,在这种情况下)。这样做最好的查询是什么样子?



请注意,我可以处理数以千计的孩子,所以我不想把数组切片在数千个例行的基础上。



编辑:没有必要经过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆