我想简化我的选择器:
'#a #b a[href^=mailto], .c .d a[href^=mailto]'
收件人:
':matches(#a #b, .c .d) a[href^=mailto]'
仅使用 jQuery 选择器是否可行?或者我必须这样做:
$('#a #b, .c .d').find('a[href^=mailto]')
哪个不那么灵活。
最佳答案
jQuery 不提供等价于 :any()
/:matches()
的选择器(其中 :any()
是原始形式和 it was first implemented internally in Gecko and WebKit )。如果您不能或不愿意扩展您的选择器,您确实必须分解您的选择器字符串并进行单独的方法调用。
您使用哪种方法取决于 :matches()
将出现在您的选择器中的确切位置:
如果
:matches()
单独出现在选择器字符串的开头,在任何组合器之前,如问题中:':matches(#a #b, .c .d) a[href^=mailto]'
在出现
:matches()
的地方替换$()
,将选择器的剩余部分传递给.find()
,.children()
、.next()
或.nextAll()
取决于:matches()
之后的组合子(分别为后代、子代、+
或~
)。转自问题:
$('#a #b, .c .d').find('a[href^=mailto]')
如果
:matches()
单独出现在组合子之后,例如:'#a #b > :matches(.c, .d)'
在出现
:matches()
的地方替换上面方法之一:$('#a #b').children('.c, .d')
如果
:matches()
作为另一个复合选择器的一部分出现,例如:':matches(#a #b, .c .d) a[href^=mailto]:matches(.e, .f)'
在出现
:matches()
的地方替换.filter()
:$('#a #b, .c .d').find('a[href^=mailto]').filter('.e, .f')
关于jquery - jQuery 有类似 :any or :matches pseudo-class? 的东西吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339353/