javascript Textarea:是否可以检测到失去焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8920313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Textarea: can it detect the loss of focus
提问by sazr
In HTML or Javascript is it possible to detect when a textarea looses focus?
在 HTML 或 Javascript 中,是否可以检测 textarea 何时失去焦点?
I have a text area & on loosing focus I want to read the data in a textarea & check if its valid.
我有一个文本区域,并且在失去焦点时我想读取文本区域中的数据并检查其是否有效。
Maybe...
或许...
<textarea onloosefocus="myFunct();">
</textarea>
// or
var isTyping = false;
function onKeyUp( event )
{
setTimeout( function()
{
isTyping = false;
}, 100);
setTimeout( function()
{
if (!isTyping)
validateTextArea();
}, 500);
}
function onKeyDown( event )
{
isTyping = true;
}
<textarea onkeydown="onKeyDown(e);" onkeyup="onKeyUp(e);">
</textarea>
回答by ThiefMaster
The event you are looking for is the blur
event, available through the onblur
attribute:
您正在寻找的blur
事件是事件,可通过onblur
属性获得:
<textarea onblur="myFunct();"></textarea>
However, it's much better if you attach events properly, e.g. through jQuery:
但是,如果您正确附加事件,则更好,例如通过 jQuery:
$('#id_of_your_textarea').on('blur', function(e) {
// your code here
});
回答by Matthew
You can also use the onblur
attribute for the textarea if you don't have access to jquery
onblur
如果您无权访问 jquery,也可以使用textarea的属性