Только некоторые элементы могут быть сфокусированы, например, <a>
и <input>
.Для других элементов вы должны реализовать это самостоятельно.
// window.addFocusToElem(elem, callbacks) - returns id for removeFocusFromElem
// window.removeFocusByID(id)
// in IE <= 8 the blur events get fired in the wrong order!
// callbacks: { onFocus: function(e) {}, onBlur: function(e) {} } - both methods are optional
(function() {
var addEvent, removeEvent;
(function() {
// sometimes in IE <= 8 the window.blur event isn't fired when the
// window loses the focus but instead it is fired when the window gets
// the focus back again. This requires some hacking - and because
// 'fireEvent' in window === false it even requires some more hacking.
var hasFocus = true;
var queue = [];
addEvent = function(node, evtType, callback) {
if('addEventListener' in node)
node.addEventListener(evtType, callback, false);
else { // IE <= 8
if(evtType === 'blur') {
queue.push(callback);
}
node.attachEvent('on' + evtType, callback);
}
}
removeEvent = function(node, evtType, callback) {
if('removeEventListener' in node)
node.removeEventListener(evtType, callback, false);
else { // IE <= 8
if(evtType === 'blur') {
var length = queue.length;
while(length--) {
if(callback === queue[ length ]) {
queue.splice(length, 1);
break;
}
}
}
node.detachEvent('on' + evtType, callback);
}
}
// IE <= 8
if('documentMode' in document && document.documentMode <= 8) {
setInterval(function() {
if(!document.hasFocus() && hasFocus) {
hasFocus = false;
for(var o in queue) {
queue[ o ](document.createEventObject());
}
}
}, 100);
addEvent(window, 'focus', function(e) {hasFocus = true;});
}
})();
function doClick(node, evtType) {
if('click' in node) { // most Browser (HTML-DOM)
node.click();
} else if('createEvent' in document) { // at least Chrome (16)
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
node.dispatchEvent(e);
} else {
}
}
var id = 0;
var queue = [];
window.addFocusToElem = function addFocusToElem(elem, callbacks) {
var _id = id++;
var entry = queue[ _id ] = {
elem: elem,
onFocus: function(e) {
removeEvent(entry.elem, 'click', entry.onFocus);
addEvent(document, 'click', entry.onBlur);
if('onFocus' in callbacks &&
typeof callbacks.onFocus === 'function') {
callbacks.onFocus(e);
}
},
onBlur: function(e) {
var node = 'target' in e ? e.target : e.srcElement;
while(node) {
if(node === entry.elem) {
break;
}
node = node.parentNode;
}
if(!node) {
removeEvent(document, 'click', entry.onBlur);
addEvent(area, 'click', entry.onFocus);
if('onBlur' in callbacks &&
typeof callbacks.onBlur === 'function') {
callbacks.onBlur(e);
}
}
}
};
addEvent(elem, 'click', entry.onFocus);
addEvent(window, 'blur', function(e) {
doClick(elem.parentNode);
});
addEvent(document, 'keyup', function(e) {
if(e.keyCode === 9) { // tab
var node = 'target' in e ? e.target : e.srcElement;
while(node) {
if(node === elem) {
doClick(elem);
break;
}
node = node.parentNode;
}
if(!node) {
doClick(elem.parentNode);
}
}
});
return _id;
};
window.removeFocusByID = function removeFocusByID(id) {
if(id in queue) {
var entry = queue[ id ];
removeEvent(entry.elem, 'click', entry.onFocus);
removeEvent(document, 'click', entry.onBlur);
delete queue[ id ];
return true;
}
return false;
};
})();
Использование:
<div style="background-color: Aqua; width: 100px; height: 50px" id='area'>
<input name="rb" type="radio">Foo<br>
<input name="rb" type="radio">Bar
</div>
<script type='text/javascript'>
var id = addFocusToElem(document.getElementById('area'), {
onFocus: function(e) {
// statements
},
onBlur: function(e) {
// statements
}
});
// removeFocusByID(id);
</script>
jsFiddle