简体   繁体   中英

javascript jquery create and listen for event

Is there an simple example of how to create an event in javascript and listen for it?

Trying to create a Complete event, similar to the ajaxComplete. Want other stuff on the page to show messages when Complete event is fired.

//create a hanlder:
$('.myselector, .myDue').on('myEvent',function(){
  alert('custom event fired');
});

//trigger the event a few ways:
$('.myselector').trigger('myEvent');

$('.myselector').click(function(){
    $(this).trigger('myEvent');
});

function hitTheTrigger(whatwashit){
   $(whatwashit).trigger('myEvent');
}

hitTheTrigger('.myDue');
hitTheTrigger('.myselector');

test it out here: http://jsfiddle.net/MarkSchultheiss/rTNTg/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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