HTML5也添加了辅助管理DOM焦点的功能.

元素获得焦点的方式有页面加载,用户输入和代码中调用的focus()方法.

而document.activeElement属性保存着当前获得焦点的引用.

1  var button = document.getElementById("guoBtn");
2             button.focus();
3             alert(document.activeElement == button); //true

另外就是document.hasFocus()方法了.这个方法用于确定文档是否获得了焦点.

1 alert(document.hasFocus()); //true

document.hasFocus(),通过检测文档是否获得了焦点,可以知道用户是否正在与页面交互.

查询文档获知那个元素获得了焦点,以及确定文档是否获得了焦点,是一个极大的进步,至少不用向过去那样靠猜测了

相关文章:

  • 2021-12-29
  • 2021-06-13
  • 2022-02-10
  • 2021-06-10
  • 2021-11-10
  • 2022-12-23
  • 2021-08-17
  • 2021-08-30
猜你喜欢
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
相关资源
相似解决方案