如何使用 jquery 查找匹配选择器类和数据属性

Nan*_*Lal 1 javascript jquery jquery-selectors

当选择器类和数据属性都匹配时,有没有办法得到 true ?

查找匹配的下拉菜单,例如:

`<select class="type-featured" data-featured="normal">`
....
`<select class="type-featured" data-featured="classical">`
....
`<select class="type-featured" data-featured="modern">`
Run Code Online (Sandbox Code Playgroud)

查找选择,类等于类型特征且数据特征值等于正常 - 这是代码:

$('.style-wrapper').each( function() {
    var $styles = $(this);
    if ( $styles.parents().find('select').is('.type-featured, [data-featured=normal]') ) {
    // do something here
    }
});
Run Code Online (Sandbox Code Playgroud)

我得到的是 TRUE 无论 select 有type-featured类还是data-featured="normal", data-featured="classical", data-featured="modern"

如果匹配任何一个选择器,它似乎是 TRUE。

使用 .is 函数可以得到我想要的结果吗?可能正在使用匿名函数,例如:

.is( function() {
//here some logic to result
}); 
Run Code Online (Sandbox Code Playgroud)

Rou*_*ica 7

如果您部署 jQuery:

$('select.type-featured[data-featured="normal"]').each(function(){

    // [... DO SOMETHING...]

});
Run Code Online (Sandbox Code Playgroud)

<select>您将仅在具有以下特征的元素上运行该函数:

  • class="type-featured"
  • data-featured="normal"