有没有可能在JavaScript中更改document.activeElement? [英] Is it possible to change document.activeElement in JavaScript?

查看:1805
本文介绍了有没有可能在JavaScript中更改document.activeElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过编程方式在JavaScript中更改 document.activeElement 属性的值?

解决方案

在IE中,使用要成为活动元素的元素的 setActive() 方法。在支持 activeElement 的其他浏览器中,可以使用元素的 focus()方法,只要元素能够接收焦点(表单元素,可编辑的元素, tabindex set的元素)。

如果你想要将activeElement设置回默认值(大多数浏览器中的< body> 元素),只需调用活动元素的 blur() method:

  document.activeElement.blur(); 


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

解决方案

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();

这篇关于有没有可能在JavaScript中更改document.activeElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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