本文介绍了如何在mootools中替换bindwithevent 1.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何替换Mootools 1.3中的bindWithEvent函数,文档中的示例非常基本:

I wonder how to replace the bindWithEvent function in Mootools 1.3, the example in the documentation is very basic:

Element.addEvent('click', function(e){
myFunction.bind(bind, [e]);});

但是,如果我需要将一个参数传递给事件处理程序呢?这是Mootools 1.2中的方式:

But, what about if I need to pass a param to the event handler? This is the way in Mootools 1.2:

Element.addEvent('click', function(e, param) { e.stop(); alert(param) }.bindWithEvent(this,['text']);

Any关于如何在Mootools 1.3中替换它的想法。

Any idea on how to replace this in Mootools 1.3.

更新:我发现了一个非常难看的解决方案,但至少它在我找到内置解决方案时有效:

Update: I found a very ugly solution, but a least it works while I find a built-in solution:

Element.addEvent('click', function(e){ e.stop(); this.bind.myFunc(this.param);}.bind({bind:this, param: 'text'}));


推荐答案

el.addEvent('click', function(event){
    myFunction(event, param1, param2); // can use .pass and bind this again
}.bind(this));

很难解释为什么它已被弃用。

it's hard to explain why it got deprecated though.

类上下文中的示例:

var foo = new Class({
    initialize: function(el) {
        document.id(el).addEvent('click', function(event){
            this.foo(event, "hello");
        }.bind(this));
    },
    foo: function(event, what) {
        console.log(event, this); // this is the class instance
        alert(what);
    }
});

new foo("foo");

这篇关于如何在mootools中替换bindwithevent 1.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 02:19