我正在为 jQuery 写一个小插件,到目前为止我得到了这个:
(function($){
$.fn.extend({
someFunction: function() {
return this.each(function() {
var obj = $(this);
obj.focus(someInternalFunction(obj));
});
}
});
function someInternalFunction(obj) {
};
})(jQuery);
问题是,当我将 someFunction 附加到对象时,对象获得焦点并且 someInternalFunction 在焦点事件上的绑定(bind)失败。
然后我尝试绑定(bind)函数以将函数调用包装在另一个函数中:
obj.focus(function() {
someInternalFunction($(this));
});
此代码有效,但一点也不漂亮。是否可以将函数绑定(bind)到焦点上而不将其包装在其他函数中?
最佳答案
$.fn.bindFocus() = function(){
var internalFunction = function(){
var $this = $(this),
self = this;
// try do stuff here
};
return this.each(function(){
$(this).bind('focus', internalFunction);
});
}
$('#myElement').bindFocus();
希望对您有所帮助?
美国东部时间。抱歉,第一次误会:)
关于javascript - 焦点上的 jQuery 绑定(bind)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1517219/