1

我有一张如下表:

<table>
  <tr>
    <td><input id="c1" type="text" class=""></td>
    <td><input id="c2" type="text" class="readonly"></td>
    <td><input id="c3" type="text" class=""></td>
  </tr>
</table>

我正在实现一些自定义单元格键盘导航,例如,我希望能够将焦点移动到键盘输入上的新单元格,但跳过与给定选择器匹配的单元格(在#c1应该将焦点移动到下一个不是的单元格.readonly,同样,在开头按左箭头#c3应该将焦点移动到最后一个不是的单元格.readonly

我的代码可以处理键盘事件,但是我的 jQuery 选择器无法移动焦点。我可以轻松地在循环中做我想做的事,但我正在寻找一种更优雅的方式来做到这一点。我当前使用的选择器(例如,将“previous”从 移动#c3#c2,(打算是#c1))是:

$(el).parent('td').prev('td').find('input').focus();

我基本上该怎么做:

Element -> Parent 'td' -> Closest previous 'td' containing an 'input' element without the 'readonly' class

4

1 回答 1

2

试试这个

$(el).closest('td').prevAll('td:has(input:not(.readonly))').first().find('input').focus();
于 2012-10-25T17:42:45.243 回答