【问题标题】:Mootools class: call a class method from an event handlerMootools 类:从事件处理程序调用类方法
【发布时间】:2013-12-29 08:35:06
【问题描述】:

是否可以从事件处理程序中调用类方法? 我的意思是,像这样:

initialize: function(options) {
    /* initial options */
    this.setOptions(options);
    this.area = (this.options.status == 'out') ? $(this.options.loggedoutArea) : $(this.options.loggedinArea);
    this.button = $(this.options.buttonArea);
    this.button.addEvent('click', function(){
        this.showHideBanner();
    });       
},

showHideBanner: function() {
    this.area.setStyle('display', 'none');
},   

在此处查看完整示例http://jsfiddle.net/dvzj6/

【问题讨论】:

    标签: class events scope mootools


    【解决方案1】:

    我设法使用此代码找到了解决方案:

    initialize: function(options) {
        /* initial options */
        this.setOptions(options);
        this.area = (this.options.status == 'out') ? $(this.options.loggedoutArea) :     $(this.options.loggedinArea);
        this.button = $(this.options.buttonArea);
        this.button.addEvent('click', function(){
            this.showHideBanner();
        }.bind(this));       
    },
    

    另请参阅:http://jsfiddle.net/uA3Ak/

    【讨论】:

    • 你也可以this.button.addEvent('click', this.showHideBanner.bind(this)) - 避免额外的 fn 调用/换行。
    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 2012-08-16
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多