JQuery 需要多个选择器
JQuery required multiple selectors
有没有办法让这里的选择器成为必填项:
var $el = $('.top').children(':gt(2), :contains("word")');
根据Multiple-Selectors,会找到匹配这两个选择器中的any的元素。
如何找到匹配 all 选择器而不是 any 选择器的元素? jQuery( "selector1, selector2, selectorN" )
您可以简单地将两个选择器相互连接:
var $el = $('.top').children(':gt(2):contains("word")');
最容易组合的选择器:
$('.a.b.c').css({'color':'blue'});
另一种方法是使用 filter()
function with the first selector and add the selector 2 to N inside the filter()
using the is()
函数 - 请参见下面的演示:
/*This combines the three selectors .a .b and .c*/
$('.a').filter(function(){
return $(this).is('.b') && $(this).is('.c')
}).css({'color':'blue'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="a">1</div>
<div class="b">2</div>
<div class="c">3</div>
<div class="a b">4</div>
<div class="a c">5</div>
<div class="b c">6</div>
<div class="a b c">7</div>
有没有办法让这里的选择器成为必填项:
var $el = $('.top').children(':gt(2), :contains("word")');
根据Multiple-Selectors,会找到匹配这两个选择器中的any的元素。
如何找到匹配 all 选择器而不是 any 选择器的元素? jQuery( "selector1, selector2, selectorN" )
您可以简单地将两个选择器相互连接:
var $el = $('.top').children(':gt(2):contains("word")');
最容易组合的选择器:
$('.a.b.c').css({'color':'blue'});
另一种方法是使用 filter()
function with the first selector and add the selector 2 to N inside the filter()
using the is()
函数 - 请参见下面的演示:
/*This combines the three selectors .a .b and .c*/
$('.a').filter(function(){
return $(this).is('.b') && $(this).is('.c')
}).css({'color':'blue'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="a">1</div>
<div class="b">2</div>
<div class="c">3</div>
<div class="a b">4</div>
<div class="a c">5</div>
<div class="b c">6</div>
<div class="a b c">7</div>