javascript - 在动态图上创建一个事件

时间:2013-01-10 12:50:42

标签: javascript html graph

我使用visualize函数创建了一个动态条形图,visualize.css图表的值现在通过HTML表格给出了我想在该图表上执行一个事件。

1 个答案:

答案 0 :(得分:0)

创建自定义事件:

(function() {

    /**
     * Small event class, can be instanciated as an object or using .call used to decorate other
     * Objects with events funcitonality. 
     */
    MyEvent.Event = function() {

        this.events = {};

        this.bind = function(eventName, func) {

            if (!this.events[eventName]) {
                this.events[eventName] = [];
            }

            this.events[eventName].push(func);
        };

        this.trigger = function(eventName, args, scope) {

            if (this.events[eventName]) {

                var len = this.events[eventName].length,
                    funcArgs = (jQuery.isArray(args)) ? args : [args];

                // This will go from back to front through the array not sure if this really matters?
                while (len--) {         
                    this.events[eventName][len].apply(scope || window, funcArgs);
                }
            }
        }

        this.removeAllEvents = function() {
            this.events = {};
        }
    };

    // Page level events class
    MyEvent.pageEvents = new MyEvent.Event();

})();

触发它:

MyEvent.pageEvents.trigger("Method-name", "data")); 

最后听听:

MyEvent.pageEvents.bind("Method-name", function(Data) {
    //Code goes here
});  

使用JQUERY?

看看trigger and bind