简体   繁体   English

按下向下键时可以触发另一个键吗?

[英]Can I trigger another key when I press the down key?

I want to trigger the Tab key when I press the down key . 当我按下向下键时,我想触发Tab

So when I press the down arrow key it should have the same effect if use I the Tab key. 因此,当我按下Tab键时,按下向下箭头键应该具有相同的效果。

Does anyone know for my issue a jquery or javascript code? 有谁知道我的问题的jQuery或JavaScript代码? :) :)

No, you can't trigger a different key based on a keypress, for good reasons. 不,出于充分的原因,您无法基于按键触发其他按键。 Otherwise, you could trick browsers into auto-completing passwords or other sensitive information or even cause a password manager to do a global autofill. 否则,您可能会诱使浏览器自动填充密码或其他敏感信息,甚至导致密码管理器执行全局自动填充。

That said, if all you want to do is, say, make the down key jump from one form field to the next, like the tab key does, that's easy. 就是说,如果您只想使向下键从一个表单字段跳到下一个表单字段(如Tab键一样),那很容易。 Just intercept the keypress in question and check whether it is the down key (charCode 40). 只需拦截有问题的按键并检查它是否为向下键(字符代码40)。 If it is, execute whatever code you want to use to handle the event. 如果是这样,请执行要用于处理事件的任何代码。

Hi this could possibly fix your problem 嗨,这可能会解决您的问题

arrow keys are only triggered by onkeydown, not onkeypress keycode for Down is 40 箭头键仅由onkeydown触发,而不是onkeypress的Down键码为40

function convertDownToTab() {
  if(event.keyCode==40) {
    event.keyCode = 9;
  }
}
document.onkeydown = convertDownToTab;

Or Using jQuery, you can do this When you press the down arrow key (keyCode 40), the next input receives the focus. 或使用jQuery,您可以执行此操作当您按下向下箭头键(键代码40)时,下一个输入将获得焦点。

$('input, select').keydown(function(e) {
if (e.keyCode==40) {
    $(this).next('input, select').focus();
}
});

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM