1

我想做这样的事情......

var Color = Class.create({
    initialize: function() {
        this._color = "white";
        this.observe("evt: colorChanged", this.colorChanged.bind(this));
    },
    changeColor: function(color) {
        this._color = color;
        this.fire("evt: colorChanged");
    },
    colorChanged: function(event) {
        alert("You change my color!");
    }
});
var a = new Color().changeColor("blue");

为什么colorChange永远不会分派自定义事件,而我需要使用thisDOM 元素,而不是document.observe?

在我的代码中,我想知道哪个类使用哪个类调度事件,如果我必须使用或其他一些 DOM 元素event.target,我不能。document:(

我一直在使用 Actionscript 3,这就是我学会在类中处理自定义事件的方法。Javascript 呢?

4

1 回答 1

0

这应该有效:

var Color = Class.create({
    initialize: function() {
        this._color = "white";
        Event.observe(document, "evt: colorChanged", this.colorChanged.bind(this));
    },
    changeColor: function(color) {
        this._color = color;
        Event.fire(document, "evt: colorChanged", this, false);
    },
    colorChanged: function(event) {
        alert("You change my color!");
    }
});
var a = new Color().changeColor("blue");
于 2010-03-05T17:21:25.577 回答