如何在textarea中使用keydown事件? [英] How to use keydown event in textarea?

查看:356
本文介绍了如何在textarea中使用keydown事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不习惯使用javascript,但我厌倦了在工作中手动重复尝试。当我在论坛中写作时,我需要一个快速的短命令,比如Ctrl-Alt-z,将一些文本插入到textarea对象中。

I am not very used to using javascript but I have gotten sick of manually repeating a tast at work. When I write in a discussion forum I need a quick short command, like Ctrl-Alt-z, to insert some text into a textarea object.

我已经写了一个在文本光标insertAtCursor(text)处插入文本的函数。 textarea的ID是content。

I have already written a function that inserts the text at the text cursor insertAtCursor(text). The ID of the textarea is "content".

我知道如何解决检查组合键的问题。我的问题基本上是检查任何键盘输入。

I know how to solve the problem of checking for key combinations. The problem I have is basically to check for any keyboard input at all.

我尝试过以下方法:

document.keydown(function(event){
  alert("Test");
});

然而,它不起作用。

提前致谢!

推荐答案

如果您正在寻找跨浏览器,我认为您将度过难关解决方案。以下是帮助您的方法: http://www.quirksmode.org/dom/events/keys .html

I think you're going to have a tough time if you're looking for cross-browser solutions. Here's something to help you: http://www.quirksmode.org/dom/events/keys.html

基本上,你需要这样的东西:

Basically, you'd want something like this:

document.getElementById('content').addEventListener('keydown', function (e){
    // Do your key combination detection
}, false);

关于事件的MDN。可能更有帮助

这篇关于如何在textarea中使用keydown事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆