2017-01-24 37 views
0

匹配两个选择器的类和数据属性时,有没有办法获得true?如何使用jquery查找匹配选择器的类和数据属性

查找匹配的下拉例如:

`<select class="type-featured" data-featured="normal">` 
.... 
`<select class="type-featured" data-featured="classical">` 
.... 
`<select class="type-featured" data-featured="modern">` 

查找选择,类等于输入功能和数据功能的值等于正常的 - 这里是代码:

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

我是否选择了TRUE有type-featured类或data-featured="normal", data-featured="classical", data-featured="modern"

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

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

.is(function() { 
//here some logic to result 
}); 

回答

0

代替is(...)尝试hasClass() - 这确定是否有任何匹配的元素被分配给定的类。

+0

的.hasClass( )方法将返回true,如果该类被分配给e字元素。这里是类和数据分区 –

2

如果部署了jQuery:

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

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

}); 

你只能运行在具有这些<select>元素的功能:

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

我写这个例子的代码。我原来的代码很大,很难解释。我可不好好解释一下。好的,如果我们有选择,输入和textarea然后如何匹配? –

+0

使用逗号,可以指定任意数量的选择器以合并为单个结果。请参阅:https://api.jquery.com/multiple-selector/ **例如。**'('select.type-featured [data-featured =“normal”],input.type-featured [data-featured =“normal”],textarea.type-featured [data-featured =“normal”]')' – Rounin

相关问题