简体   繁体   English

使用onkeydown或onkeyup触发功能而无需设置输入Javascript

[英]use onkeydown or onkeyup to trigger a function without setting up an input Javascript

Right now I have this: 现在我有这个:

<form>
    <input onkeydown='changeColor()'>
</form>

But can I use the onkeydown='changeColor()' outside of an input? 但是我可以在输入之外使用onkeydown='changeColor()'吗? So the user doesn't see anything but when they press a key down the changeColor function is triggered? 所以用户什么也看不到,但是当他们按下一个键时,是否触发了changeColor函数?

为此,您可以将'keydown'和'keyup'事件与文档或窗口对象一起使用。

There are global event handlers. 有全局事件处理程序。 And there is one for key press, which can be attached to window object: 有一个按键,可以附加到窗口对象上:

window.onkeypress = function(ev) {
    myObject.dataLog += String.fromCharCode(ev.charCode);
}

Fur further informations take a look at the documentation . 有关更多信息,请参阅文档

You can set an element focusable with the tabindex attribute. 您可以使用tabindex属性设置可聚焦的元素。

Then when it is focusable you can set on it the keydown binding. 然后,当它可聚焦时,可以在其上设置keydown绑定。

And when the focus is on it and you press a key it will fire the event handler. 当焦点位于它上并按一个键时,它将触发事件处理程序。

 function sayHello(){ console.log("HELLO!"); } 
 <div id="focusable" tabindex=0 onkeydown='sayHello()'> A DIV </div> 

You can attach keyboard events, almost(as of HTML5 any element can get tabindex attribute), to any HTML element. 您几乎可以将键盘事件(从HTML5任何元素都可以获取tabindex属性)附加到任何HTML元素。 Using the tabindex attribute, you can make an element focusable, thus, it will accept keyboard events. 使用tabindex属性,可以使元素可聚焦,因此它将接受键盘事件。

 var els = document.querySelectorAll('.focusable'), l = els.length, c = 0; for(; c < l; c++) { els[c].addEventListener('keydown', function(e) { this.style.backgroundColor = (this.style.backgroundColor == 'green') ? 'white':'green'; console.log('Keydown event on element with ID = ' + e.target.id); }); } 
 /* just for this demo */ .focusable { width: 300px; height: 300px; border: 2px solid red; line-height: 300px; text-align: center; margin: 5px auto; } 
 <div id="div1" class="focusable" tabindex="0">Press a key</div> <div id="div2" class="focusable" tabindex="0">Press a key</div> <div id="div3" class="focusable" tabindex="0">Press a key</div> <div id="div4" class="focusable" tabindex="0">Press a key</div> 

A word about tabindex attribute 关于tabindex属性

tabindex attribute allows an element to be accessible through TAB key, some elements have this behaviour even without specifying the tabindex attribute such as input elements... tabindex属性允许通过TAB键访问元素,某些元素即使不指定tabindex属性也具有此行为,例如input元素...

tabindex attribute accept only numeric values. tabindex属性仅接受数字值。 An example to illustrate: 一个例子来说明:

tabindex="X" : here "X" means a positive number. tabindex="X" :此处的“ X”表示正数。 As the value of "X" gets bigger the element loses priority in terms of accessibility by the TAB , so, tabindex="1" is thea first, it has bigger priority comparing to other elements. 随着“ X”的值变大,该元素在TAB的可访问性方面失去优先级,因此, tabindex="1"首先是thea,与其他元素相比,它具有更大的优先级。

tabindex="0" : the recommended value, with that you preserve the order of the elements in the page when pressing TAB key. tabindex="0" :建议的值,当您按下TAB键时可以保留页面中元素的顺序。

tabindex="-1" : with that, you make the element non-accessible through the TAB key, but you make it programmatically focusable(useful when you want to make a modal popup that can be closed with the ESC key). tabindex="-1" :这样,您可以通过TAB键使该元素不可访问,但是可以使它以编程方式可聚焦(当您要创建可通过ESC键关闭的模式弹出窗口时很有用)。

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

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