【问题标题】:Check if an input field has focus in vanilla JavaScript检查输入字段是否在 vanilla JavaScript 中具有焦点
【发布时间】:2015-06-08 16:50:19
【问题描述】:

使用 jQuery,我可以像这样测试输入字段是否具有焦点:

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

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

【问题讨论】:

标签: javascript html input focus


【解决方案1】:

这个问题在这里得到了回答:Javascript detect if input is focused

取自上述答案:

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

【讨论】:

  • 使用示例:if (document.activeElement.tagName === "INPUT") console.log("An input is focused");
猜你喜欢
  • 2017-12-17
  • 2016-01-19
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
相关资源
最近更新 更多