sim*_*neL 1 javascript jquery object
也许我错过了一些东西,但我无法弄清楚如何从点击回调中调用Listviewclass的方法之一.
这是代码:
function Listview(el, cb) {
this.element = el;
this.callback = cb;
this.select = function (element, index) {
...
};
this.selectElement = function (element) {
...
};
this.unselectCurrentElement = function () {
...
};
this.element.find('li').click(function () {
// Here I want to call for example the selectElement method
// but how?
// The This keyword reference the "li" element
});
this.element.addClass("Listview");
this.select(this.element, 0);
};
Run Code Online (Sandbox Code Playgroud)
你有几个选择:
既然您click无论如何都要内联处理函数,请使用处理程序关闭的局部变量:
var inst = this;
this.element.find('li').click(function () {
// `inst` is your instance, `this` is the element
inst.selectElement(this);
});
Run Code Online (Sandbox Code Playgroud)使用jQuery proxy:
this.element.find('li').click($.proxy(function (e) {
// `this` is your instance, `e.currentTarget` is the element
this.selectElement(e.currentTarget);
}, this));
Run Code Online (Sandbox Code Playgroud)使用ES5 Function#bind:
this.element.find('li').click(function (e) {
// `this` is your instance, `e.currentTarget` is the element
this.selectElement(e.currentTarget);
}.bind(this));
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1312 次 |
| 最近记录: |