javascript - jquery 选择器查询分组 ors 和 ands

标签 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 进行分组,然后在末尾抛出一个 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;
})

未经测试,我编造了条件,但您应该明白要点。

jQuery filter documentation

关于javascript - jquery 选择器查询分组 ors 和 ands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803312/

相关文章:

javascript - 如何定位 JSON 文件中的特定对象

Javascript使用递归从给定数组生成数组

javascript - 如何在 shell 表达式中添加条件?

javascript - 如何让父DIV的子DIV充当父DIV

javascript - 在 WooCommerce 存档产品类别描述上添加“阅读更多/阅读更少”按钮

java - 如何获取从 JComboBox 中选择的项目的索引?

java - 如何引用Java中BeanEditForm(tapestry5)中使用的bean的属性?

java - 创建带有子节点的不可选择的 DefaultTreeModel 节点

javascript - 如何在文本编辑器中实时协作处理单个文档

javascript - ES6 : How to have a getter/setter for an object AND its properties