简体   繁体   English

无法立即调用onKeyPress函数

[英]onKeyPress Function Not Being Called Immediately

  document.getElementById('my').onkeypress = function(event) { this.value = this.value.toUpperCase() } 
  <input id='my' type="text"> 

Can someone explain to me what the this variable points to in the code above? 有人可以向我解释一下以上代码中this变量指向的内容吗? Does it point to the input html element I grabbed from the document.getElementById('my) ? 它是否指向我从document.getElementById('my)抓取的输入html元素? Or does it point to the window object in the browser? 还是指向浏览器中的窗口对象? Also what is the event parameter passed into the anonymous function? 另外,传递给匿名函数的event参数是什么?

For some reason when I run this code the value of each character is uppercased except the last char. 由于某种原因,当我运行此代码时,除最后一个字符外,每个字符的值均大写。 So for example when I enter: a. 例如,当我输入: The character a is not uppercased immediately it is only uppercased when I enter another letter. 字符a不会立即大写,仅当我输入另一个字母时才大写。 For example: Ab. 例如:Ab。 Now b is not uppercased until I enter another letter after b: ABc. 现在b不会大写,直到我在b之后输入另一个字母:ABc。 And this continues on. 并继续下去。 Can someone please explain why this happen? 有人可以解释为什么会这样吗?

Can someone explain to me what the this variable points to in the code above? 有人可以向我解释一下以上代码中此变量指向的内容吗? Does it point to the input html element I grabbed from the document.getElementById('my)? 它是否指向我从document.getElementById('my)抓取的输入html元素? Or does it point to the window object in the browser? 还是指向浏览器中的窗口对象? Also what is the 'event' parameter passed into the anonymous function? 另外,传递给匿名函数的'event'参数是什么?

  • this refers to the input element this是指输入元素
  • it's not an anonymous function, it's the declaration of the onkeypress function 它不是匿名函数,而是onkeypress函数的声明
  • event is a parameter of the above function more info event是以上函数的参数更多信息

The unexpected(?) behaviour is because the code is executed before the input field is updated, so only the previous value gets uppercased. 意外的(?)行为是因为在更新输入字段之前执行了代码,因此只有先前的值才大写。 To avoid it, you can use onkeyup event which fires after you release a key. 为了避免这种情况,您可以使用onkeyup事件,该事件在释放密钥后将触发。

See it here: 在这里看到它:

 document.getElementById('my').onkeypress = function(event) { this.value = this.value.toUpperCase() } document.getElementById('my2').onkeyup = function(event) { this.value = this.value.toUpperCase() } document.getElementById('my3').oninput = function(event) { this.value = this.value.toUpperCase() } 
 <input id='my' type="text" placeholder="onkeypress"> <input id='my2' type="text" placeholder="onkeyup"> <input id='my3' type="text" placeholder="oninput"> 

Thanks to @www139 for bringing up oninput in the comments as it is indeed the best solution for modern browsers Please note it's HTML5-only and you can't get info like the key code, identifier, etc... 感谢@ www139在注释中显示oninput ,因为它确实是现代浏览器的最佳解决方案。请注意,它仅支持HTML5,并且您无法获取键码,标识符等信息。

Here is a simple definition of the key events references from w3c. 这是w3c中关键事件引用的简单定义。

Tip: The order of events related to the onkeypress event: 提示:与onkeypress事件相关的事件顺序:

onkeydown onkeypress onkeyup Note: The onkeypress event is not fired for all keys (eg ALT, CTRL, SHIFT, ESC) in all browsers. onkeydown onkeypress onkeyup注意:在所有浏览器中,不会为所有键(例如ALT,CTRL,SHIFT,ESC)触发onkeypress事件。 To detect only whether the user has pressed a key, use onkeydown instead, because it works for all keys. 要仅检测用户是否已按下某个键,请改用onkeydown,因为它适用于所有键。

This explains the oninput event; 这解释了oninput事件; http://help.dottoro.com/ljhxklln.php http://help.dottoro.com/ljhxklln.php

The oninput event has support of all the major browsers including IE 9 and later. oninput事件支持所有主要浏览器,包括IE 9和更高版本。

oninput better suits your needs than the "second-best" onkeyup because onkeyup only fires when the key is released. 与“次优” onkeyup相比,oninput更适合您的需求,因为onkeyup仅在释放键时才触发。 When you type, the new value is entered as soon as it is pressed, however it seems to add the new value to the box after the function fires. 当您键入时,新值一被按下就被输入,但是似乎在函数触发后将新值添加到框中。 I will continue to experiment because I have another idea of how to do this but oninput seems like the best key event for this at the moment. 我将继续尝试,因为我对如何执行此操作有另一个想法,但是oninput似乎是目前最好的关键事件。

 document.getElementById('my').oninput = function(event) { this.value = this.value.toUpperCase() } 
 <input id='my' type="text"> 

UPDATE UPDATE

You can still actually use the onkeypress event but you need to use a timeout to wait for the new value to be inserted into the text box before executing the function. 您实际上仍然可以使用onkeypress事件,但是在执行功能之前,您需要使用超时来等待新值插入到文本框中。

Here is another code snippet.... 这是另一个代码段。

 document.getElementById('my').onkeypress = function(event) { var elem = this; window.setTimeout(function() { elem.value = elem.value.toUpperCase() }, 0); } 
 <input id='my' type="text"> 

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

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