简体   繁体   English

如何使用jQuery发出事件?

[英]How to emit an event with jquery?

i'm trying to create a simple class to emit an event in certain moment. 我正在尝试创建一个简单的类以在特定时刻发出事件。 I get this javascript error : this.emit is not a function 我收到此JavaScript错误:this.emit不是函数

This is the code: 这是代码:

var class_test = function(){};
class_test.prototype = {
   some_property: null,

    doSomething: function(msg) {
            this.some_property = msg;
            this.emit("somethingHappened");
        }
};


var test = new class_test();
test.doSomething('Example');

test.addEventListener('somethingHappened',function(){
    alert("Event");
},false);

Any suggestion?,Thanks! 有什么建议吗,谢谢!

with jQuery ,since you requested it : 使用jQuery,因为您已请求它:

var class_test = function(){
   this.dispatcher = $({});
};
class_test.prototype = {
   some_property: null,

    doSomething: function(msg) {
            this.some_property = msg;
            this.dispatcher.trigger("somethingHappened");
        }
};


var test = new class_test();


test.dispatcher.on('somethingHappened',function(){
    alert("Event");
});

test.doSomething('Example');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM