jQuery-在多个条件下过滤



我正在尝试根据多种条件构建过滤器,我想组合数据和CSS属性。

示例:我有多个DIV,具有称为"数据组"的数据属性,在共享相同数据组值的DIV中,我想定位具有相对定位的DIV。

<div id="a" data-group="1" style="position:fixed">
<div id="b" data-group="1" style="position:relative"> 
<div id="c" data-group="1" style="position:fixed">
<div id="d" data-group="0" style="position:fixed">

我想创建具有数据组= 1和固定位置的所有DIVS。

当我只有一个条件时,我倾向于使用以下内容:

common = $("div").filter(function() {  return $(this).attr("data-group") === 1;          });

...但是如何应用第二条件?

谢谢!

laurent

您可以这样做类似于现在的方式:

common =  $("div").filter(function() {
  return $(this).attr("data-group") == 1 && this.style.position == 'fixed' ;
});

或单线:

common = $('div[data-group="1"][style="position:fixed"]');

filter fixed

common = $("div").filter(function() {  
  return $(this).attr("data-group") === 1 && $(this).css('position') === 'fixed';
});

过滤器relative

common = $("div").filter(function() {  
  return $(this).attr("data-group") === 1 && $(this).css('position') === 'relative';
});

最新更新