检测JavaScript中的焦点丢失 [英] Detect loss of focus in JavaScript
问题描述
我想要检测JavaScript中任意元素是否松散,所以我可以构建一个类似于jEdit的在线编辑工具。我不能依赖于这个库的jQuery,所以我需要一个本地的方法来做这个。
I want to be able to detect when arbitrary elements loose focus in JavaScript, so I can build an in-line editing tool similar to jEdit. I can't depend on jQuery for this library, so I need a native method for doing it.
我看着onblur,这似乎是对的, MDN有一个注释,表明它可能是IE特定的?
I looked at onblur, which would seem to be the right thing but MDN has a note that suggests it may be IE specific?
与MSIE相反 - 几乎所有类型的元素都接收
模糊事件 - Gecko浏览器上几乎所有的元素都不能与此事件一起工作
。
In contrast to MSIE--in which almost all kinds of elements receive the blur event--almost all kinds of elements on Gecko browsers do NOT work with this event.
其中元素会/不会使用这个,有没有更好的方法来检测这些东西?
Which elements will/wont work with this, is there a better way to detect such things?
如果模糊工作,文档应该是 https://developer.mozilla.org/en-US/docs/DOM/element.onblur be更改?
If blur works, should the docs at https://developer.mozilla.org/en-US/docs/DOM/element.onblur be changed?
推荐答案
您可以在body元素上使用 onfocusout
。 onblur
和 onfocusout
之间的区别在于后者起泡,因此您不必为每个节点,你想要的。
You can use an onfocusout
on the body element. The difference between onblur
and onfocusout
is that the latter bubbles up, so you don't have to install it for every node that you want.
但是 onfocusout
不是跨浏览器,是IE的功能,而不是标准。目前的浏览器中设置 onblur
并不知道有什么问题,除了不起泡。
However onfocusout
is not cross-browser and is IE's feature, not a standard. I don't know of any problems with setting onblur
in current browsers, except that it doesn't bubble.
当发生模糊时,唯一需要收到通知的是在您看到更改时轮询 document.activeElement
并触发事件。请参阅检测哪种表单输入有焦点使用JavaScript或jQuery
The only hack to be notified whenever a blur occurs is for you to poll the document.activeElement
and firing events when you see changes. See Detect which form input has focus using JavaScript or jQuery
这篇关于检测JavaScript中的焦点丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!