0
我有一些代码在元素上的“模糊”事件上运行。 该代码需要实际使用activeElement,因此实际上不能从“模糊”事件中运行。如何在Javascript中创建“关于模糊”或“模糊”事件?
我希望我能创建一个这样的事件。目的是在“模糊”事件之前“willblur”事件将会发生。
var lastTouchedElement;
$('body').on('click', preBlurHandler);
$('body').on('focusin', postFocusHandler);
$('.inp1').on('willblur', function() {alert('willblur');});
$('.inp1').on('blur', function() {alert('blur');});
function preBlurHandler(ev) {
if (!lastTouchedElement) {
postFocusHandler(ev);
return;
}
var focussingElement = ev.target;
if (lastTouchedElement === focussingElement || $.contains(lastTouchedElement, focussingElement)) {
//focus is staying in the same place, do nothing
return;
}
$(lastTouchedElement).trigger('willblur', [ev, {
target: lastTouchedElement,
focussingElement: focussingElement
}]);
}
function postFocusHandler(ev) {
lastTouchedElement = document.activeElement;
}
完整的代码中的jsfiddle在https://jsfiddle.net/t0un95jt/3/
但它不工作。事实上,它甚至不是很接近。
Help me StackOverflow;你是我唯一的希望。
Focusin,[focusout](https://developer.mozilla.org/en-US/docs/Web/Events/focusout) – Jonathan