【问题标题】:how to call the button click event in post create dojo AMD module如何在 post create dojo AMD 模块中调用按钮单击事件
【发布时间】:2023-03-21 17:49:01
【问题描述】:

我在 dojo AMD 模块中创建了新的 dojo 应用程序。我需要将一个脚本文件中的我的按钮单击调用到另一个脚本文件。但是当我单击按钮时,它会显示空值错误,我的示例代码如下:

我的函数文件代码是:

define(["dojo/_base/declare"  . . .], // defaultDuration
    function (declare . . ) { 


    var mycode = declare([ContentPane, _WidgetBase, _TemplatedMixin], { 

        toggle: function () {
        //here my function 
        },  
        constructor: function (params /) { 
        },

        postCreate: function () { 
        }
    });

    return mycode;
});

按钮点击事件:

define(["dojo/_base/declare" . . . ],
    function (declare . . .) {

        var evet = declare([dijit._WidgetBase, dijit._TemplatedMixin], { 
            _div: null, 

            constructor: function (div) {
                this._div = div; 
            },

            postCreate: function () { 
                this.inherited(arguments);

                var markbutton = new Button({
                    label: "Mark", 
                }, this.markButtonNode); 

                markbutton.on("click", function (evt) {
                    this._div.toggle(); // error here _div is undefined. 
                });
            }
        });

        return evet;
    });

【问题讨论】:

  • 你找到解决办法了吗?

标签: javascript dojo toggle amd


【解决方案1】:

您需要连接范围,this 才能工作。

markbutton.on("click", lang.hitch(this, function (evt) {
    this._div.toggle(); 
}));

【讨论】:

    【解决方案2】:

    好吧,你可以将作用域存储在一个变量中,然后在你的函数中访问这个变量......

    试试这个:-

              var _this = this;
              markbutton.on("click", function (evt) {
                    _this._div.toggle(); 
                });
    

    在这种方法中,不需要“dojo/_base/lang”...

    希望这会对你有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      相关资源
      最近更新 更多