我有一个表,如下:
<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
发布于 2012-10-25 17:42:45
尝尝这个
$(el).closest('td').prevAll('td:has(input:not(.readonly))').first().find('input').focus();
https://stackoverflow.com/questions/13074132
复制相似问题