Check if an input field has focus in vanilla JavaScript

前端 未结 1 1256
故里飘歌
故里飘歌 2021-02-12 12:26

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

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

How do I do that without using jQuery?

1条回答
  •  臣服心动
    2021-02-12 12:53

    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
    

    0 讨论(0)
提交回复
热议问题