我想通过js访问输入或者输入标签的类,作为选择器,这样我就可以通过键盘箭头访问下一个输入元素。通过提及类名,它是不可访问的,但是在删除除输入标签之外的所有标签并仅将“输入”作为选择器之后,此代码可以工作,就像在 Fiddle 中一样。
但是当我尝试使用类名访问它时,它无法访问。
这是我的 Fiddle
使用的代码:
<table id="foo">
<tr>
<td height="30px">
<input type="text" class="nav_class" value="">
</td>
<td height="30px">
<input type="text" class="nav_class" value="">
</td>
<td height="30px">
<input type="text" class="nav_class" value="">
</td>
<td height="30px">
<input type="text" class="nav_class" value="">
</td>
</tr>
</table>
<script>
$(".nav_class").keydown(function (e) {
if (e.keyCode == 40) {
alert('a');
$(this).next(".nav_class").focus();
}
});
</script>
最佳答案
这是因为输入被包装在 td 中。您需要使用:
$(this).parent().next().find(".nav_class").focus();
完整代码:
$(".nav_class").keydown(function (e) {
if (e.keyCode == 40) {
alert('a');
$(this).parent().next().find(".nav_class").focus();
}});
关于javascript - 使用哪个选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24949271/