【问题标题】:Select every element that is not inside another element选择不在另一个元素内的每个元素
【发布时间】:2011-05-14 14:39:11
【问题描述】:
<img src="xxx" alt="xxx" title="xxx">
<div class="buttons">
    <a href="xxx"><img src="xxx" alt="xxx" title="xxx"></a>
</div>

我需要编写一个 jQuery 选择器,它将只选择标题属性在 .buttons div 之外的图像。我知道要选择具有标题属性的图像,我需要使用以下内容:

$("img[title]")

我知道 jQuery 中有类似 :not() 选择器之类的东西,但我找不到将它们组合在一起以获得确切结果的方法。

【问题讨论】:

    标签: jquery css dom jquery-selectors


    【解决方案1】:

    您可以获取结果集,然后使用.not() 对其进行过滤,如下所示:

    $("img[title]").not(".buttons img")
    

    或者使用:not() 在同一个选择器中进行过滤(但在旧版浏览器中可能会慢一些),如下所示:

    $("img[title]:not(.buttons img)")
    

    【讨论】:

    • 有没有办法在不重复原始选择器('img')部分的情况下做到这一点?我正在尝试查找不在.container-to-exclude 内部的错误,并且错误可能是多个选择器,因此我将它们用逗号分隔,例如.Error, .Errors, .Warning,我不想说$('.Error, .Errors, .Warning').not('.container-to-exclude .Error, .container-to-exclude .Errors, .container-to-exclude .Warning')
    • @jbyrd 你可以使用' *' 代替' img'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多