[英]HTMLElement Custom Event
I create a custom event ex. 我创建一个自定义事件ex。 var event = new Event('hello');
, add listener to nodes ,将侦听器添加到节点
div1.addEventListener('hello', /*cb1*/);
div2.addEventListener('hello', /*cb2*/);
div3.addEventListener('hello', /*cb3*/);
and how to dispatch
my event? 如何dispatch
我的事件? I would like to call event.dispatch()
and cb1
, cb2
and cb3
should be called 我想调用event.dispatch()
和cb1
, cb2
和cb3
应该被调用
Just create Event
using new Event('hello');
只需使用new Event('hello');
创建Event
and dispatch them one by one for each div. 并为每个div逐一分发。
div1.addEventListener('hello', function(){ console.log( "Event called for " + this.id ); }); div2.addEventListener('hello', function(){ console.log( "Event called for " + this.id ); }); div3.addEventListener('hello', function(){ console.log( "Event called for " + this.id ); }); var event = new Event('hello'); div1.dispatchEvent( event ); div2.dispatchEvent( event ); div3.dispatchEvent( event );
<div> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.