We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
转载自:YvetteLau/Blog#28
自定义事件有三种方法,一种是使用new Event(), 另一种是createEvent('CustomEvent'), 另一种是new customEvent()
new Event()
createEvent('CustomEvent')
new customEvent()
1、使用new Event() 获取不到event.detail
event.detail
let btn = document.querySelector('#btn'); let ev = new Event('alert', { bubbles: true, //事件是否冒泡;默认值false cancelable: true, //事件能否被取消;默认值false composed: false }); btn.addEventListener('alert', function (event) { console.log(event.bubbles); //true console.log(event.cancelable); //true console.log(event.detail); //undefined }, false); btn.dispatchEvent(ev);
2、使用createEvent('CustomEvent')(DOM3) 要创建自定义事件,可以调用createEvent('CustomEvent'),返回的对象有 initCustomEvent 方法,接受以下四个参数:
let btn = document.querySelector('#btn'); let ev = btn.createEvent('CustomEvent'); ev.initCustomEvent('alert', true, true, 'button'); btn.addEventListener('alert', function (event) { console.log(event.bubbles); //true console.log(event.cancelable);//true console.log(event.detail); //button }, false); btn.dispatchEvent(ev);
3、使用new customEvent()(DOM4) 使用起来比createEvent('CustomEvent')更加方便
var btn = document.querySelector('#btn'); /* * 第一个参数是事件类型 * 第二个参数是一个对象 */ var ev = new CustomEvent('alert', { bubbles: 'true', cancelable: 'true', detail: 'button' }); btn.addEventListener('alert', function (event) { console.log(event.bubbles); //true console.log(event.cancelable);//true console.log(event.detail); //button }, false); btn.dispatchEvent(ev);
EventTarget类型有一个单独的属性handlers,用于存储事件处理程序(观察者)。
addHandler() 用于注册给定类型事件的事件处理程序;
fire() 用于触发一个事件;
removeHandler() 用于注销某个事件类型的事件处理程序。
function EventTarget(){ this.handlers = {}; } EventTarget.prototype = { constructor:EventTarget, addHandler:function(type,handler){ if(typeof this.handlers[type] === "undefined"){ this.handlers[type] = []; } this.handlers[type].push(handler); }, fire:function(event){ if(!event.target){ event.target = this; } if(this.handlers[event.type] instanceof Array){ const handlers = this.handlers[event.type]; handlers.forEach((handler)=>{ handler(event); }); } }, removeHandler:function(type,handler){ if(this.handlers[type] instanceof Array){ const handlers = this.handlers[type]; for(var i = 0,len = handlers.length; i < len; i++){ if(handlers[i] === handler){ break; } } handlers.splice(i,1); } } } //使用 function handleMessage(event){ console.log(event.message); } //创建一个新对象 var target = new EventTarget(); //添加一个事件处理程序 target.addHandler("message", handleMessage); //触发事件 target.fire({type:"message", message:"Hi"}); //Hi //删除事件处理程序 target.removeHandler("message",handleMessage); //再次触发事件,没有事件处理程序 target.fire({type:"message",message: "Hi"});
The text was updated successfully, but these errors were encountered:
No branches or pull requests
自定义 DOM 事件(不考虑IE9之前版本)
自定义事件有三种方法,一种是使用
new Event()
, 另一种是createEvent('CustomEvent')
, 另一种是new customEvent()
1、使用
new Event()
获取不到
event.detail
2、使用
createEvent('CustomEvent')
(DOM3)要创建自定义事件,可以调用
createEvent('CustomEvent')
,返回的对象有 initCustomEvent 方法,接受以下四个参数:3、使用
new customEvent()
(DOM4)使用起来比
createEvent('CustomEvent')
更加方便自定义非 DOM 事件(观察者模式)
EventTarget类型有一个单独的属性handlers,用于存储事件处理程序(观察者)。
addHandler() 用于注册给定类型事件的事件处理程序;
fire() 用于触发一个事件;
removeHandler() 用于注销某个事件类型的事件处理程序。
The text was updated successfully, but these errors were encountered: