问题描述
我真的在寻找可以与当前所有流行浏览器(IE9,Chrome,Firefox,Safari和IE8兼容)使用的东西.
I'm really looking for something that works with all current popular browsers (IE9, Chrome, Firefox, Safari) and all the way back to IE8.
尽管,我一直在寻找一种方法来将焦点移到Flash移动对象上而失去焦点,但我发现执行此操作的所有历史方法都失败了.我认为这是另一个安全问题.
Although, I've been looking for a way to set focus on a Flash move object after it has lost focus, I've found that all historical ways of doing this fail. I assume it is yet another security issue.
因此,我现在正在寻找如何监视document.activeElement的某种更改事件(尽管更改"实际上并没有发生).
So, I'm now looking for how to monitor change events of some sort for the document.activeElement (though "change" doesn't really occur).
推荐答案
@James的答案正确无误.我还添加了更多详细信息,使其与focus
事件一起使用,使其成为一个完全可行的解决方案.
While @James's answer above is correct. I've added more details to make it a completely working solution along with the use of focus
event too.
<html>
<body>
<input type="text" id="Text1" name ="Text1" value=""/>
<input type="text" id="Text2" name ="Text2" value=""/>
<SELECT><OPTION>ASDASD</OPTION><OPTION>A232</OPTION></SELECT>
<INPUT TYPE="CHECKBOX" id="Check1"/>
<INPUT type="TEXT" id="text3"/>
<input type="radio"/>
<div id="console"> </div>
<textarea id="textarea1"> </textarea>
<script>
var lastActiveElement = document.activeElement;
function detectBlur() {
// Do logic related to blur using document.activeElement;
// You can do change detection too using lastActiveElement as a history
}
function isSameActiveElement() {
var currentActiveElement = document.activeElement;
if(lastActiveElement != currentActiveElement) {
lastActiveElement = currentActiveElement;
return false;
}
return true;
}
function detectFocus() {
// Add logic to detect focus and to see if it has changed or not from the lastActiveElement.
}
function attachEvents() {
window.addEventListener ? window.addEventListener('focus', detectFocus, true) : window.attachEvent('onfocusout', detectFocus);
window.addEventListener ? window.addEventListener('blur', detectBlur, true) : window.attachEvent('onblur', detectBlur);
}
attachEvents();
</script>
</body>
</html>
这篇关于是否有跨浏览器解决方案用于监视document.activeElement何时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!