获取jQuery中最上层的子孙后代 [英] Get most upper descendant descendant(s) in jQuery

查看:96
本文介绍了获取jQuery中最上层的子孙后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

page = $(#page)我想获得所有指定的SVG,但不是后续的内容。

 < div id =page> 
< div ...>
< svg> / * THIS ONE * /
< rect ...>
< circle ...>
< svg> / * NOT THIS ONE * /
< foo> ...< / foo>
< / svg>
< / svg>
< / div>
< div ...>
< div ...>
< svg> / * THIS ONE * /
< rect ...>
< circle ...>
< svg> / * NOT THIS ONE * /
< foo> ...< / foo>
< / svg>
< / svg>
< / div>
< / div>
< / div>

EDIT 我更新了标记以清楚指定我的意思。



一个解决方案是使用:not(svg svg),我发现丑陋和性能很重。 >

解决方案

更新问题后



  $('#page svg:not(:has(svg))'); 

和您自己的

  $('#page svg:not(svg svg)'); 

性能方面(检查测试)他们大致相同,没有明确的胜出者






原始答案



对于特定示例

  $('#page> svg:first')

  $('#page svg:first')





但请考虑以下情况

 < div id =page> 
< div>
< p>
< svg id =svg-1>< / svg>
< / p>
< svg id =svg-2>< / svg>
< / div>
< / div>这是可能的(这两个svgs包装在别的东西

如果是,您是否需要#svg-2



$ c>> 版本, $('#page svg:first')版本将返回#svg-1


From page=$("#page") I want to get all the specified SVGs but not the subsequent inner ones.

<div id="page">
    <div ...>
        <svg> /* THIS ONE */
            <rect ...>
            <circle ...>
            <svg> /* NOT THIS ONE */
                <foo> ... </foo>
            </svg>
        </svg>
    </div>
    <div ...>
        <div ...>
            <svg> /* THIS ONE */
                <rect ...>
                <circle ...>
                <svg> /* NOT THIS ONE */
                    <foo> ... </foo>
                </svg>
            </svg>
        </div>
   </div>
</div>

EDIT I've updated the markup to clearly specify what I mean.

One solution is to use :not(svg svg) which i find ugly and performance heavy.

解决方案

After updated question

The only solutions are

$('#page svg:not(:has(svg))');

and your own

$('#page svg:not(svg svg)');

Performance-wise (check out the test) they are about the same with no clear winner


original answer

For the particular example

$('#page > svg:first')

or

$('#page svg:first')

will suffice.


But consider the following case

<div id="page">
   <div>
     <p>
        <svg id="svg-1"></svg>
     </p>
     <svg id="svg-2"></svg>
   </div>
</div>

Is this case possible (both svgs wrapped in something else) ? and if so, do you want #svg-2 ?

This case is not handled by the > version at all, and the$('#page svg:first') version will return the #svg-1

这篇关于获取jQuery中最上层的子孙后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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