我正在试图找到正确的选择器,以便在单击任何“Hello”单元格时更改背景为蓝色,但是如果单击“Goodbye”,则不更改。向“Hello”单元格添加类是一种可能的选项,但并不理想。
$(function() {
$('#myTable td').click(function() {
$('#myTable').addClass('unfocused');
});
});
.unfocused {
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='myTable'>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>
<table id='otherTable'>
<tr>
<td>Something</td>
</tr>
</table>
</td>
</tr>
我已经尝试过:
$('#myTable td').not('#otherTable td').click(....
$('#myTable td').not('#otherTable').click(....
这些方法都不起作用。
附上一个fiddle。
#otherTable
的<th>
吗?如果不是,您需要扩展一些if's
:/ - Krzysiek Szymczakth
中放置一个table
? - Andrei V