Javascript jQuery$.proxy作用域
可能是个小问题,但我找不到解决办法。以下代码:Javascript jQuery$.proxy作用域,javascript,jquery,scope,Javascript,Jquery,Scope,可能是个小问题,但我找不到解决办法。以下代码: $.extend(Module.prototype, { initialize : function() { ... $('.afx-list a').on('click', $.proxy(function() { this.onClickEventHandler(/* argumentIwantTohave - clicked element */); re
$.extend(Module.prototype, {
initialize : function() {
...
$('.afx-list a').on('click', $.proxy(function() {
this.onClickEventHandler(/* argumentIwantTohave - clicked element */);
return false;
}, this));
...
},
...
onClickEventHandler : function(/* argumentIwantTohave */) {
var currentScrollTop = 0,
destinationScrollTop = 0,
offsetScrollTop = 0;
currentScrollTop = $(document).scrollTop();
destinationScrollTop = $('a[name=' + $(this).attr('href').split('#')[1] + ']').offset().top;
// Resolvin the problem with missing 1px when scrolling to destination
if(currentScrollTop < destinationScrollTop) {
offsetScrollTop = 1;
} else {
offsetScrollTop = -1;
}
$('body').animate({
scrollTop: destinationScrollTop + offsetScrollTop
}, 1000);
},
});
但是我不知道应该为“clicked”属性放置什么引用 您能否为代理函数提供一个
事件
参数,并将其传递给onClickEventHandler
。然后使用event.target
属性?是的,这似乎有效,这是我的解决方案。谢谢。您甚至可以将代理调用减少到$.proxy(this.onClickEventHandler,this)
,只需为单击处理程序设置事件
参数。是的,您是对的-现在看起来更具可读性
$('.afx-list a').on('click', $.proxy(function() {
this.onClickEventHandler(/* argumentIwantTohave - clicked element */);
return false;
}, { clicked : /* dont know what is the refference */, module : this}));