处理任何
我正在尝试处理有人在我的页面上的任何
那么,有没有一种方法可以处理我页面上的所有
I am trying to handle the case where someone presses ENTER on any <textarea>
on my page. Not all of the <textarea>
's are on the page when it loads (they can be added dynamically through user events), so I don't know each <textarea>
id at page load (and therefore can't use jQuery to listen to each event by id). I also tried to use the JS onkeydown
event, but I haven't been able to find a way to detect which key caused the onkeydown
event to fire.
So, is there a way to handle all <textarea>
keydown's on my page, along with which key caused the event, or is there an elegant solution I seem to be missing? Some global event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
委托
。您可以指定将容纳所有这些动态文本区域的body
或任何container
。Use
delegate
. You can specify thebody
or anycontainer
which will hold all these dynamic textareas.或者您可以使用 delegate ,
它的作用是将侦听器应用于执行时和将来的所有文本区域那个例子。因此,如果 DOM 添加了任何文本区域,它也会将该按键绑定到它们。
Or you can use delegate
What this does is apply the listener to all textareas on execution and in the future of that instance. So if any textareas are added by the DOM, it will bind that keypress to them also.
event.which
告诉您哪个键。live 上的文档。
关于事件的文档。
event.which
tells you which key.Docs on live.
Docs on event.
为什么不使用 JQuery,然后在“动态”添加文本区域时添加侦听器/处理程序......有很多用于处理 onKeyPress 事件的示例
Why don't you use JQuery and then as the textarea's are added "dynamically" add the listener / handler .... there are plenty of examples around for handling the onKeyPress event