迭代 N 层子级

发布于 2024-10-12 15:06:31 字数 662 浏览 1 评论 0原文

这看起来像是一些很巧妙的东西,可以“内置”到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

没有心的人 2024-10-19 15:06:32
var lvlFunc = function(elmt, depth) {
    if(depth > 0) {
        elmt.children().each(function(i, e){
            // do stuff on the way down
            lvlFunc($(this), --depth);
            // do stuff on the way out
        });
        // do stuff
    }
};

lvlFunc(divToLookAt, 3);

如果“东西”执行的顺序很重要,请确保将“做东西”代码放在正确的位置。

var lvlFunc = function(elmt, depth) {
    if(depth > 0) {
        elmt.children().each(function(i, e){
            // do stuff on the way down
            lvlFunc($(this), --depth);
            // do stuff on the way out
        });
        // do stuff
    }
};

lvlFunc(divToLookAt, 3);

Make sure that you put your "do stuff" code in the right location if matters which order the "stuff" is performed in.

虫児飞 2024-10-19 15:06:31

这是一个很棒的问题,因为级别很深。 查看小提琴

将其转换为插件。

激活

$('#div').goDeep(3, function(deep){ // $.fn.goDeep(levels, callback)
    // do stuff on `this`
});

插件

$.fn.goDeep = function(levels, func){
    var iterateChildren = function(current, levelsDeep){
        func.call(current, levelsDeep);

        if(levelsDeep > 0)
            $.each(current.children(), function(index, element){
                iterateChildren($(element), levelsDeep-1);
            });
    };

    return this.each(function(){
        iterateChildren($(this), levels);
    });
};

This is an awesome question because of the levels deep catch. Check out the fiddle.

Converted this to a plugin.

Activate

$('#div').goDeep(3, function(deep){ // $.fn.goDeep(levels, callback)
    // do stuff on `this`
});

Plugin

$.fn.goDeep = function(levels, func){
    var iterateChildren = function(current, levelsDeep){
        func.call(current, levelsDeep);

        if(levelsDeep > 0)
            $.each(current.children(), function(index, element){
                iterateChildren($(element), levelsDeep-1);
            });
    };

    return this.each(function(){
        iterateChildren($(this), levels);
    });
};
一梦浮鱼 2024-10-19 15:06:31

这个问题太棒了:-)

如果你知道你的 DOM 不是太大,你可以找到所有的后代并过滤掉那些不符合条件的:

var $parent = $('#parent');
var $childrenWithinRange = $parent.find('*').filter(function() {
  return $(this).parents('#parent').length < yourMaxDepth;
});

之后,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:

var $parent = $('#parent');
var $childrenWithinRange = $parent.find('*').filter(function() {
  return $(this).parents('#parent').length < yourMaxDepth;
});

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.

來不及說愛妳 2024-10-19 15:06:31

您应该能够使用 all-selector< i>(docs)child-选择器(文档)多重选择器(docs),如下所示:

示例: http://jsfiddle.net/mDu9q/1/

$('#start > *,#start > * > *,#start > * > * > *').doSomething();

...或者如果您想要以 3 级深度为目标,您可以这样做:

示例: http:// /jsfiddle.net/mDu9q/2/

$('#start > * > * > *').doSomething();

这两个选择器都对 querySelectorAll 有效,这意味着受支持的浏览器的性能大幅提升。

You should be able to just do it with the all-selector(docs), the child-selector(docs) and multiple-selector(docs) like this:

Example: http://jsfiddle.net/mDu9q/1/

$('#start > *,#start > * > *,#start > * > * > *').doSomething();

...or if you only wanted to target the children 3 levels deep, you could do this:

Example: http://jsfiddle.net/mDu9q/2/

$('#start > * > * > *').doSomething();

Both of these selectors are valid for querySelectorAll, which means big performance boost in supported browsers.

北城孤痞 2024-10-19 15:06:31

这个问题听起来像是答案可能是 XPATH。我对浏览器支持不太了解,但在 XPATH 中,您只需要创建一个类似

/*/*/*/*

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

/*/*/*/*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文