你好,我试图检测使用命名空间来聚焦元素。这是我的工作代码:jQuery .on()在命名空间中不起作用,而不是在.live()中起作用
(function($){
searchBox = {
element: $('div#block-search-form input[name=search_block_form]'),
focus: function(){
this.element.live('focusin',function(){
console.log('in');
});
this.element.live('focusout', function(){
console.log('out');
});
}
}
$(document).ready(function(){
searchBox.focus();
});
})(jQuery);
我的问题是如何使它不使用.live()?
编辑
为了使它工作,我不得不作出一个元素的功能,现在一切工作正常。当我没有时,它没有正确的上下文。 我的新代码:
(function($){
searchBox = {
element: function(){return $('div#block-search-form input[name=search_block_form]')},
init: function(){
this.focus();
},
focus: function(){
var that = this;
that.element().on({
'focusin': function(){
/*code*/
},
'focusout': function(){
/*code*/
}
});
}
}
$(document).ready(function(){
searchBox.init();
});
})(jQuery);
['this.element.focusout()'](https://api.jquery.com/focusout/) – Liam
使用'。对() '作为'live()'弃用于:1.7,删除:1.9 –
您使用的是什么版本的jQuery? –