箭头键导航输入div内的文本

时间:2016-02-27 08:34:46

标签: javascript

This JSFiddle显示了我的箭头键导航技巧。如何使用以下HTML跳过<br>标记?

<div class='move'>
  <input type="text" /><br>
  <input type="text" /><br>
  <input type="text" />
</div>

1 个答案:

答案 0 :(得分:0)

<div class='move'><input /></div>
<div class='move'><input /></div>
<div class='move'><input /></div>

$(document).keydown(
  function(e) {
    if (e.keyCode == 39) {
      $(".move>input:focus").parent().next().children().focus();
    }
    if (e.keyCode == 37) {
      $(".move>input:focus").parent().prev().children().focus();
    }
  }
);

但我是这样做的:

$('.move>input').keydown(
  function(e) {
    if (e.keyCode == 39) {
      $(this).parent().next().children().focus();
    }
    if (e.keyCode == 37) {
      $(this).parent().prev().children().focus();
    }
  }
);