custom event in javascript and jquery

zyip發表於2014-05-02

 

javascript:

// add a eventListener
document.addEventListener("abc", function(){alert('this is abc handler'}, false);

//fire event
document.dispatchEvent(new CustomEvent("abc", {
            detail: {
                message: msg,
                time: new Date(),
            },
            bubbles: true,
            cancelable: true
        }));

 

jQuery

$(document).on('abc',function(){alert('this is abc handler on document'});

$.event.trigger({
    type: "abc",
    message: "Hello World!",
    time: new Date()
});

 

 

reference:

How to Create Custom Events in JavaScript

http://www.sitepoint.com/javascript-custom-events/

How to Create Custom Events in jQuery

http://www.sitepoint.com/jquery-custom-events/

 

 

相關文章