jQuery有类似于:any或:matches pseudo-class? [英] Does jQuery have something like the :any or :matches pseudo-class?

查看:91
本文介绍了jQuery有类似于:any或:matches pseudo-class?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简化我的选择器:

'#a #b a[href^=mailto], .c .d a[href^=mailto]'

To:

':matches(#a #b, .c .d) a[href^=mailto]'

这可能只是使用jQuery选择器吗?或者我必须这样做:

Is this possible just using jQuery selectors? Or do I have to do this:

$('#a #b, .c .d').find('a[href^=mailto]')

这不够灵活。

推荐答案

jQuery不提供等效于的选择器:any() / :matches()(其中:any()是原始格式,它首先在Gecko和WebKit内部实现)。

jQuery does not provide a selector equivalent of :any()/:matches() (of which :any() was the original form and it was first implemented internally in Gecko and WebKit). You will indeed have to break up your selector strings and make separate method calls, if you are unable or unwilling to expand your selector.

您使用哪种方法取决于您使用哪种方法取决于您的选择器字符串。如果您不能或不愿意扩展您的选择器, :matches()会出现在您的选择器中:

Which method(s) you use depends on where exactly :matches() would appear in your selector:


  • 如果:matches()本身出现在选择器字符串的开头,在任何组合器之前,如问题:

  • If :matches() appears by itself in the beginning of the selector string, before any combinators, as in the question:

':matches(#a #b, .c .d) a[href^=mailto]'

替换 $()其中:matches() ,并将选择器的其余部分传递给 .find() .children() $ :matches() c。> .next() .nextAll $ c>(子孙,子, + )。

Substitute $() where :matches() appears, and pass the remainder of the selector to .find(), .children(), .next() or .nextAll() depending on the combinator that follows :matches() (descendant, child, +, or ~ respectively).

从问题中转出:

$('#a #b, .c .d').find('a[href^=mailto]')


  • 如果:matches()在组合器后显示,例如:

  • If :matches() appears by itself after a combinator, for example:

    '#a #b > :matches(.c, .d)'
    

    替换上述方法之一:matches()出现:

    Substitute one of the above methods where :matches() appears:

    $('#a #b').children('.c, .d')
    


  • 如果:matches ()显示为另一个复合选择器的一部分,例如:

  • If :matches() appears as part of another compound selector, for example:

    ':matches(#a #b, .c .d) a[href^=mailto]:matches(.e, .f)'
    


    $ b b

    替换 .filter()其中:matches()出现:

    $('#a #b, .c .d').find('a[href^=mailto]').filter('.e, .f')
    


  • 这篇关于jQuery有类似于:any或:matches pseudo-class?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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