处理任何

发布于 2024-11-29 07:32:31 字数 482 浏览 0 评论 0原文

我正在尝试处理有人在我的页面上的任何

那么,有没有一种方法可以处理我页面上的所有

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傾旎 2024-12-06 07:32:31

使用委托。您可以指定将容纳所有这些动态文本区域的 body 或任何container

$("body").delegate("textarea", "keydown",function(e){
  // code logic goes here
  if(e.which == 13){
     //Enter key down    
  }
});

Use delegate. You can specify the body or any container which will hold all these dynamic textareas.

$("body").delegate("textarea", "keydown",function(e){
  // code logic goes here
  if(e.which == 13){
     //Enter key down    
  }
});
拔了角的鹿 2024-12-06 07:32:31
$("textarea").live("keydown",function(){
  // process
})

或者您可以使用 delegate

它的作用是将侦听器应用于执行时和将来的所有文本区域那个例子。因此,如果 DOM 添加了任何文本区域,它也会将该按键绑定到它们。

$("textarea").live("keydown",function(){
  // process
})

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.

十秒萌定你 2024-12-06 07:32:31

event.which 告诉您哪个键。

$('textarea').live("keydown", function(e) {
    // e.which is which key, e.g. 13 == enter
});

live 上的文档。
关于事件的文档。

event.which tells you which key.

$('textarea').live("keydown", function(e) {
    // e.which is which key, e.g. 13 == enter
});

Docs on live.
Docs on event.

ゝ杯具 2024-12-06 07:32:31

为什么不使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文