好吧,我现在陷入了一个非常尴尬的境地,我正在尝试为一组特定的规则创建一个选择器。
每个元素都有一系列数据属性,我试图匹配一些,查询是这样的
select all elements where data-1 contains (any of these)
and with data-1 starts with this
and with data-1 starts with that
and with data-2 is this
and with data-3 is this
and with data-3 is this.
这就是人类的语言,我遇到的问题特定于查询的第一部分,我需要有效地对一组 ors 进行分组,然后在末尾抛出一个 and
执行[data-1*="this"],[data-1*="that"][data-1^="thing"]
结果
select all elements where data-1 contains "this" or
select all elements where data-1 contains "that" and data-1 starts with "thing".
谁能帮我解决这个问题吗?最好将其作为单个查询而不是多个查询。
我希望看到的可能是([data-1*="this"],[data-1*="that"])[data-1^="thing"]
有效地将 ors 分组到查询的单个部分中,但可惜这不起作用。
最佳答案
我根本不会违背这些属性。更新元素数据(例如 $('.myclass').data('foo', 'bar')
不会更新属性。属性设置元素数据的初始值,但就是全部。
对于你的情况,我建议使用 jQuery 过滤功能。类似的东西
$('.some-class-common-to-all-candidates').filter(function(){
var d = $(this).data('1');
if( 'foo' == d ){
return true;
} else if( d > 10 ){
return true;
}
// If we get this far then no match
return false;
})
未经测试,我编造了条件,但您应该明白要点。
关于javascript - jquery 选择器查询分组 ors 和 ands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803312/