如何使用“getModifierState”焦点事件与JS? [英] How can use "getModifierState" on focus event with JS?
问题描述
$('#输入')[0] .addEventListener('focus',function(key){
if(key.originalEvent.getModifierState(CapsLock))
//做某事...
else {
//做某事..
}
});
但是我收到了这个错误Can not read property getModifierState'undefined不知道我做错了什么。
有些建议或链接要提前阅读谢谢。
有不同类型的事件对象。 getModifierState
仅为 KeyboardEvent
定义(例如 keydown
)和 MouseEvent
(例如点击
); focus
既不是。
您可以使用点击$记住你是否已经有了焦点(例如,如果点击是在一个已经被聚焦的元素中,那么你不要重复这个动作)。呃,不,这是一个可怕的建议。有很多方法可以让您的元素获得焦点,而不是点击。
I'm trying to get caps lock status on focus event of input this is my code
$('#Input')[0].addEventListener('focus', function (key) {
if (key.originalEvent.getModifierState("CapsLock"))
//do something...
else {
//do something..
}
});
But i'm getting this error "Cannot read property 'getModifierState' of undefined", and I don't know what I'm doing wrong. Some advice or link for to read thanks in advance.
There are different types of event object. getModifierState
is only defined for KeyboardEvent
(such as a keydown
) and MouseEvent
(such as a click
); focus
is neither.
You might look at using Ugh, no, that's a terrible suggestion. There are lots of ways your element might get focus other than clicks.click
and remembering whether you already had focus (e.g., so you don't repeat the action if the click is in an already-focussed element).
这篇关于如何使用“getModifierState”焦点事件与JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!