用于在文本字段/区域中键入文本的 JavaScript 事件侦听器
是否可以使用 JavaScript 侦听在特定文本字段中键入的文本,然后抓取该文本并将其保存到字符串中。好吧,如果前者可以完成,后者(例如,保存到字符串)当然是可能的。
我发现一个Java Applet可以做到这一点(http ://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/textlistener.html),但我想知道是否可以用JS完成同样的事情。
我只是在寻找一些资源/文档。
如果有人可以提供帮助,我将非常感激!
Is it possible to use JavaScript to listen for text typed in a specific text-field, and then grab that text and save it to a string. Well, the latter (e.g., saving to string) will of course be possible if the former can be done.
I found a Java Applet that does this (http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/textlistener.html), but I was wondering if the same could be done with JS.
I'm just looking for some resources/docs.
If anyone can help, I'd much appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 jQuery 很简单
simple with jQuery
我建议尝试 JQuery,它允许您绑定到您选择的任何元素的 keydown 事件: http:// api.jquery.com/keydown/
但是,您可能只想在用户完成输入后更新字符串,因此您可能需要考虑在文本字段失去焦点时更新它 - 您可以通过绑定到相同的
blur
事件 方式。编辑:是的 - 意识到我在这里有点愚蠢。
change
是您应该为此绑定的事件。I'd recommend trying JQuery which will allow you to bind to the keydown event of any element you select: http://api.jquery.com/keydown/
However, you might want to only update the string after the user has finished typing, so you might want to consider updating it whenever the textfield loses focus - which you can do by binding to the
blur
event in the same way.EDIT: Yes - realise I was being a bit stupid here.
change
is the event you should bind to for this.在普通的 javascript 中,您添加一个事件监听器:
In vanilla javascript you add an event listener: