Javascript jQuery小部件从事件处理程序调用私有函数
我有一个jQueryUI小部件,它的内容中有一个链接Javascript jQuery小部件从事件处理程序调用私有函数,javascript,jquery,widget,Javascript,Jquery,Widget,我有一个jQueryUI小部件,它的内容中有一个链接 <a href="test" data-foo="clickThis">Click me</a> 我在一个页面上有多个小部件实例,所以每个小部件都应该访问自己的instanceVariable。 我发现的一种方法是将this作为event.data传递给单击处理程序,但是我不喜欢这种解决方案,因为这只是一种变通方法 this.$elem.on('click', 'a[data-foo]', this, this._
<a href="test" data-foo="clickThis">Click me</a>
我在一个页面上有多个小部件实例,所以每个小部件都应该访问自己的instanceVariable。
我发现的一种方法是将this
作为event.data
传递给单击处理程序,但是我不喜欢这种解决方案,因为这只是一种变通方法
this.$elem.on('click', 'a[data-foo]', this, this._clickHandler);
我希望有更好的解决方案。您可以使用
将自定义执行上下文传递给单击处理程序,然后事件处理程序中的将指向小部件,以便您可以从处理程序方法调用this。_otherPrivateFunction()
this.$elem.on('click', 'a[data-foo]', $.proxy(this._clickHandler, this));
非常感谢:)节省了我很多时间:)
this.$elem.on('click', 'a[data-foo]', $.proxy(this._clickHandler, this));