Проблема с виджетом Jquery
У меня есть такой виджет
$.widget("ui.myWidget", {
//default options
options: {
myOptions: "test"
},
_create: function () {
this.self = $(this.element[0]);
this.self.find("thead th").click(function () {
this.self._headerClick(); //how do I do this!!!
});
this.self._somethingElse();
},
_headerClick: function (){
},
_somethingElse: function (){
},
.
.
.
Линия this.self._headerClick();
выдает ошибку. Это потому, что в этом контексте this
это th
элемент, который был нажат Как получить ссылку на функцию _headerClick?
2 ответа
Решение
Храните объем желаемого this
внутри переменной.
$.widget("ui.myWidget", {
//default options
options: {
myOptions: "test"
},
_create: function () {
var that = this; // that will be accessible to .click(...
this.self = $(this.element[0]);
this.self.find("thead th").click(function () {
that._headerClick(); //how do I do this!!!
});
this.self._somethingElse();
},
_headerClick: function (){
},
_somethingElse: function (){
},
Не проверено, но может быть что-то вроде этого:
_create: function () {
var self = this,
$elem = $(self.element[0]);
$elem.find("thead th").click(function() {
self._headerClick();
});
self._somethingElse();
},