简体   繁体   English

我可以使用jQuery定位多个实时子选择器

[英]Can I target multiple live child selectors with jQuery

I have the following code: 我有以下代码:

$( '#parent' ).on( 'click', '.a .b .c,.a .b .d,.a .b .e', function(){ ... });

the three selectors, to be clear are: 三个选择器,要明确的是:

.a .b .c
.a .b .d
.a .b .e

I'm trying to figure out if it's possible to reduce that somehow: 我想弄清楚是否有可能以某种方式减少它:

.a .b (.c,.d,.e)

And I know how to do it with non-live queries easily (eg with .find ), but I specifically want this event to be attached to $( '#parent' ) 我知道如何轻松地使用非实时查询(例如使用.find ),但我特别希望将此事件附加到$( '#parent' )

edit from OP in case anyone wants to use this: read the warning in my comment below 如果有人想要使用它,请从OP编辑: 在下面的评论中阅读警告

Is this what you looking for? 这是你要找的? http://forum.jquery.com/topic/feature-req-any-selector-filter http://forum.jquery.com/topic/feature-req-any-selector-filter

jQuery.expr[':'].any = function(el, i, match) {
    return jQuery.find.matches(match[3], [el]).length > 0;
};

I'm not sure that's possible but I would try getting creative with some regex: 我不确定这是可能的,但我会尝试用一些正则表达式来创造:

$('#parent').on('click', 'c,d,e'.replace(/(\w+)/g, '.a.b.$1')

The idea is to create the selector before hand. 我们的想法是事先创建选择器。

If you have control over what the class names are, you can use a naming convention. 如果您可以控制类名称,则可以使用命名约定。 Rahter than .c, .d, .e and so on, name them .cd, .ce, .cf, etc. Then you can use [class^="c-"] as the selector. Rahter比.c,.d,.e等等,将它们命名为.cd,.ce,.cf等。然后你可以使用[class^="c-"]作为选择器。

Edit with more concreteness: 更具体的编辑:

$( '#parent' ).on( 'click', '.a .b [class|="c"] ...

Answering this myself thanks to Felix: 感谢Felix,我自己回答:

jQuery.expr[':'].matches = function( elem, index, match ){
    return $( elem ).is( match[3] );
};

$( '#parent' ).on( 'click', '.a .b :matches( .c, .d, .e )', ... );

which should, for performance reasons, be optimized a bit to: 出于性能原因,应该优化一下:

.a .b nodeTypeOrOtherSelector:matches( .c, .d, .e )

One way to simplify the selector a little bit would be moving the .a .b test inside the callback: 稍微简化选择器的一种方法是在回调中移动.a .b测试:

$( '#parent' ).on('click', '.c, .d, .e', function(){
    if ($(this).closest('.a .b').length === 0) {
        // not the right element
        return;
    }
    // ...
});

But whether this is desirable from a performance or code maintenance point of view, I don't know. 但是,从性能或代码维护的角度来看,这是否可取,我不知道。

Picking up on elclanrs' solution , you can create the selector dynamically: 了解elclanrs的解决方案 ,您可以动态创建选择器:

var ancestors = '.a .b';
var selector = ancestors + ['.c', '.d', '.e'].join(', ' + ancestors);

Apart from that there is not much you can do. 除此之外,你无能为力。 CSS selectors just don't work this way. CSS选择器不能以这种方式工作。

Why not move the common .a .b ancestor selectors into the target context selector? 为什么不将常见的.a .b祖先选择器移动到目标上下文选择器中? This should work: 这应该工作:

$('#parent .a .b').on('click', '.c, .d, .e', function(){ ... });

Update: @cwolves is correct in pointing out that this would only work for static .a .b ie present on the page at the time of attaching the handler, kind of defeating the purpose of delegated events in the first place. 更新: @cwolves指出这只适用于静态.a .b即在附加处理程序时出现在页面上,这种情况首先会破坏委托事件的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM