jquery选择器查询分组ors和ands

时间:2014-07-17 12:16:09

标签: javascript jquery selection custom-data-attribute

好的,所以我在这里遇到了一个非常尴尬的局面,我正在尝试为一组特定的规则创建一个选择器。

每个元素都有一系列数据属性,我试图匹配一些,查询就像这样

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进行分组然后抛出一个结尾

执行[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分组到查询的单个部分,但是唉不起作用。

1 个答案:

答案 0 :(得分:2)

我根本不反对这些属性。更新元素数据(例如$('.myclass').data('foo', 'bar'))不会更新属性。属性设置元素数据的初始值,但这就是全部。

在你的情况下,我建议使用jQuerys过滤功能。像

这样的东西
$('.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;
})

未经测试,我补充了条件,但你应该得到要点。

jQuery filter documentation