迭代 N 层子级
这看起来像是一些很巧妙的东西,可以“内置”到 jQuery 中,但我认为它仍然值得一问。
我有一个问题,可以通过迭代元素的所有子元素轻松解决。我最近发现我需要考虑到我需要比我当前正在做的“1 级”(只需调用 .children() 一次)更深一两级的情况。
jQuery.each(divToLookAt.children(), function(index, element)
{
//do stuff
}
);
这就是我目前正在做的事情。为了深入第二层,我在为每个元素执行代码后运行另一个循环。
jQuery.each(divToLookAt.children(), function(index, element)
{
//do stuff
jQuery.each(jQuery(element).children(), function(indexLevelTwo, elementLevelTwo)
{
//do stuff
}
);
}
);
如果我想更深入,我必须重新做一遍。
这显然不好。我很想声明一个“级别”变量,然后把它全部处理好。有人对干净高效的 jQueryish 解决方案有什么想法吗?
谢谢!
This seems like something neat that might be "built into" jQuery but I think it's still worth asking.
I have a problem where that can easily be solved by iterating through all the children of a element. I've recently discovered I need to account for the cases where I would need to do a level or two deeper than the "1 level" (just calling .children() once) I am currently doing.
jQuery.each(divToLookAt.children(), function(index, element)
{
//do stuff
}
);
This is what I'm current doing. To go a second layer deep, I run another loop after doing stuff code for each element.
jQuery.each(divToLookAt.children(), function(index, element)
{
//do stuff
jQuery.each(jQuery(element).children(), function(indexLevelTwo, elementLevelTwo)
{
//do stuff
}
);
}
);
If I want to go yet another level deep, I have to do this all over again.
This is clearly not good. I'd love to declare a "level" variable and then have it all take care of. Anyone have any ideas for a clean efficient jQueryish solution?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果“东西”执行的顺序很重要,请确保将“做东西”代码放在正确的位置。
Make sure that you put your "do stuff" code in the right location if matters which order the "stuff" is performed in.
这是一个很棒的问题,因为级别很深。 查看小提琴。
将其转换为插件。
激活
插件
This is an awesome question because of the levels deep catch. Check out the fiddle.
Converted this to a plugin.
Activate
Plugin
这个问题太棒了:-)
如果你知道你的 DOM 不是太大,你可以找到所有的后代并过滤掉那些不符合条件的:
之后,jQuery 实例“$childrenWithinRange”将是所有的子元素该父
的节点位于某个最大深度内。如果你想要确切的深度,你可以切换“<”到“= =”。我可能在某个地方有一个人离开了。
This question is awesome :-)
If you know your DOM is not too gigantic, you could just find all the descendants and filter out the ones that don't qualify:
After that, the jQuery instance "$childrenWithinRange" would be all the child nodes of that parent
<div>
that are within some maximum depth. If you wanted exactly that depth, you'd switch "<" to "===". I may be off by one somewhere.您应该能够使用
all-selector
< i>(docs),child-选择器
(文档) 和多重选择器
(docs),如下所示:示例: http://jsfiddle.net/mDu9q/1/
...或者如果您仅想要以 3 级深度为目标,您可以这样做:
示例: http:// /jsfiddle.net/mDu9q/2/
这两个选择器都对
querySelectorAll
有效,这意味着受支持的浏览器的性能大幅提升。You should be able to just do it with the
all-selector
(docs), thechild-selector
(docs) andmultiple-selector
(docs) like this:Example: http://jsfiddle.net/mDu9q/1/
...or if you only wanted to target the children 3 levels deep, you could do this:
Example: http://jsfiddle.net/mDu9q/2/
Both of these selectors are valid for
querySelectorAll
, which means big performance boost in supported browsers.这个问题听起来像是答案可能是 XPATH。我对浏览器支持不太了解,但在 XPATH 中,您只需要创建一个类似
(适用于 FF、Chrome、Safari、Opera)
(还没试过)
The question sounds like the answer could be XPATH. I'm not well informed about the browser-support, but in XPATH you only need to create a path like
(works in FF,Chrome,Safari,Opera)
(didn't try it yet)