我觉得我在问一个很愚蠢的问题,但也许这不是我的日子。如果我已经有一个选定的元素,例如:
let tables = $('table');
但是现在我想在其他.some-class
上应用另一个选择器,例如tables
,但不要创建像这样的新jQuery对象
$('table.some-class')
我该怎么办?
答案 0 :(得分:3)
您需要使用.filter()
将过滤器添加到变量选择器。
let tables = $('table');
tables = tables.filter('.some-class');
tables.css('color', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>table</td>
</tr>
</table>
<table class="some-class">
<tr>
<td>table has .some-class</td>
</tr>
</table>
答案 1 :(得分:0)