检查输入字段是否在 vanilla JavaScript 中具有焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30714871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:35:31  来源:igfitidea点击:

Check if an input field has focus in vanilla JavaScript

javascripthtmlinputfocus

提问by Simon Steinberger

Using jQuery, I can test if an input field has focus like so:

使用 jQuery,我可以测试输入字段是否具有焦点,如下所示:

if ($("...").is(":focus")) {
    ...
}

How do I do that without using jQuery?

如何在不使用 jQuery 的情况下做到这一点?

回答by richardgirges

This question was answered here: Javascript detect if input is focused

这个问题在这里得到了回答:Javascript检测输入是否聚焦

Taken from the above answer:

摘自上述答案:

this === document.activeElement // where 'this' is a dom object