模拟“焦点”和“模糊”在jQuery .live()方法中 [英] Simulating "focus" and "blur" in jQuery .live() method
本文介绍了模拟“焦点”和“模糊”在jQuery .live()方法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更新:从jQuery 1.4开始, $。live()
现在支持 focusin 和焦点事件。
Update: As of jQuery 1.4, $.live()
now supports focusin and focusout events.
jQuery 目前 1 不支持模糊或焦点作为 $ .live()方法。我可以实现什么类型的解决方案来实现以下目标:
jQuery currently1 doesn't support "blur" or "focus" as arguments for the $.live() method. What type of work-around could I implement to achieve the following:
$("textarea")
.live("focus", function() { foo = "bar"; })
.live("blur", function() { foo = "fizz"; });
1 。 07/29/2009,版本1.3.2
1. 07/29/2009, version 1.3.2
推荐答案
工作解决方案:
(function(){
var special = jQuery.event.special,
uid1 = 'D' + (+new Date()),
uid2 = 'D' + (+new Date() + 1);
jQuery.event.special.focus = {
setup: function() {
var _self = this,
handler = function(e) {
e = jQuery.event.fix(e);
e.type = 'focus';
if (_self === document) {
jQuery.event.handle.call(_self, e);
}
};
jQuery(this).data(uid1, handler);
if (_self === document) {
/* Must be live() */
if (_self.addEventListener) {
_self.addEventListener('focus', handler, true);
} else {
_self.attachEvent('onfocusin', handler);
}
} else {
return false;
}
},
teardown: function() {
var handler = jQuery(this).data(uid1);
if (this === document) {
if (this.removeEventListener) {
this.removeEventListener('focus', handler, true);
} else {
this.detachEvent('onfocusin', handler);
}
}
}
};
jQuery.event.special.blur = {
setup: function() {
var _self = this,
handler = function(e) {
e = jQuery.event.fix(e);
e.type = 'blur';
if (_self === document) {
jQuery.event.handle.call(_self, e);
}
};
jQuery(this).data(uid2, handler);
if (_self === document) {
/* Must be live() */
if (_self.addEventListener) {
_self.addEventListener('blur', handler, true);
} else {
_self.attachEvent('onfocusout', handler);
}
} else {
return false;
}
},
teardown: function() {
var handler = jQuery(this).data(uid2);
if (this === document) {
if (this.removeEventListener) {
this.removeEventListener('blur', handler, true);
} else {
this.detachEvent('onfocusout', handler);
}
}
}
};
})();
在IE / FF / Chrome中测试过。应该完全按照你的预期工作。
Tested in IE/FF/Chrome. Should work exactly as you intended.
更新:拆解现在正在工作。
UPDATE: Teardowns now work.
这篇关于模拟“焦点”和“模糊”在jQuery .live()方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文