系统内发生的动作或发生的事情,系统会在事件出现时触发某种信号,提供一个自动加载某种动作的机制。
var myButton = document.getElementById('button'); myButton.onclick = function(){ alert('点击了button'); } myButton = null;
var event = new Event('myEvent'); /* var event = document.creatEvent('Event'); event.initEvent('myEvent',true,true); */ myButton.addEventListener('myEvent',function(e){ debugger; },false); myButton.dispatchEvent(event);
var event = new CustomEvent('myEvent',{ 'detail':{ time: new Date().toLocalDateString(); } }); myButton.addEventListener('myEvent',function (e){ debugger; },false); myButton.dispatchEvent(event);