检查输入字段是否在香草JavaScript中有焦点 [英] Check if an input field has focus in vanilla JavaScript

查看:121
本文介绍了检查输入字段是否在香草JavaScript中有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我如何做到这一点,而不使用jQuery?

解决方案



取自以上答案:

pre $ this === document.activeElement //其中'this'是一个dom对象


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

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

How do I do that without using jQuery?

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

Taken from the above answer:

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

这篇关于检查输入字段是否在香草JavaScript中有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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