Jquery小部件有问题

kra*_*626 3 javascript jquery jquery-ui jquery-plugins jquery-widgets

我有这样的小部件

$.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 (){
    },
.
.
.
Run Code Online (Sandbox Code Playgroud)

该行this.self._headerClick();抛出错误.这是因为在该上下文中thisth被单击的元素.如何获取_headerClick函数的引用?

Rob*_*ert 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 (){
    },
Run Code Online (Sandbox Code Playgroud)