[英]How to use keydown event in textarea?
I am not very used to using javascript but I have gotten sick of manually repeating a tast at work. 我不是很习惯使用javascript但我已经厌倦了在工作中手动重复尝试。 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.
当我在讨论论坛中写作时,我需要一个快速的短命令,如Ctrl-Alt-z,将一些文本插入到textarea对象中。
I have already written a function that inserts the text at the text cursor insertAtCursor(text). 我已经写了一个函数,在文本光标insertAtCursor(text)处插入文本。 The ID of the textarea is "content".
textarea的ID是“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.
我的问题基本上是检查任何键盘输入。
I have tried the following: 我尝试过以下方法:
document.keydown(function(event){
alert("Test");
});
However, it does not work. 但是,它不起作用。
Thanks in advance! 提前致谢!
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
这里有一些东西可以帮到你: 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);
var textarea = document.getElementById('textarea');
textarea.onkeydown = function ()
{
alert("Test");
};
Using jQuery ( delegate ). 使用jQuery( 委托 )。
$("body").delegate("textarea", "keydown",function(e){
alert("Test");
//code logic goes here
//if(e.which == 13){
//Enter key down
}
});
Or 要么
$('textarea').live("keydown", function(e) {
alert("Test");
// e.which is which key, e.g. 13 == enter
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.