如何使用“getModifierState”焦点事件与JS? [英] How can use "getModifierState" on focus event with JS?

查看:422
本文介绍了如何使用“getModifierState”焦点事件与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 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). Ugh, no, that's a terrible suggestion. There are lots of ways your element might get focus other than clicks.

这篇关于如何使用“getModifierState”焦点事件与JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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