3

私はこのようなウィジェットを持っています

$.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関数への参照を取得するにはどうすればよいですか?

4

2 に答える 2

6

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 (){
    },
于 2011-05-19T16:45:02.227 に答える
2

テストされていませんが、おそらく次のようなものです:

_create: function () {
    var self = this,
        $elem = $(self.element[0]);

    $elem.find("thead th").click(function() {
        self._headerClick();
    });

    self._somethingElse();
},
于 2011-05-19T16:44:12.957 に答える