Is it possible to change document.activeElement in JavaScript?

后端 未结 2 828
醉话见心
醉话见心 2020-12-23 15:49

Is it possible to programmatically change the value of the document.activeElement property in JavaScript?

相关标签:
2条回答
  • 2020-12-23 16:28

    You can just .focus() the element you want and it'll be the new document.activeElement.

    0 讨论(0)
  • 2020-12-23 16:44

    In IE, use the setActive() method of the element that you want to be the active element. In other browsers that support activeElement, you can use the focus() method of the element, so long as the element is capable of receiving the focus (form elements, editable elements, elements with tabindex set).

    If you want to set the activeElement back to the default (the <body> element in most browsers), just call the active element's blur() method:

    document.activeElement.blur();
    
    0 讨论(0)
提交回复
热议问题