Event Listeners

Event Listeners

The addEventListener() method attaches an event handler to an element without overwriting existing event handlers.
You can add many event handlers to one element.
You can add many event handlers of the same type to one element, i.e., two "click" events.element.addEventListener(event, function, useCapture);
The first parameter is the event‘s type (like "click" or "mousedown").
The second parameter is the function we want to call when the event occurs.
The third parameter is a boolean value specifying whether to use event bubbling or event capturing. This parameter is optional, and will be described in the next lesson.
Note that you don‘t use the "on" prefix for the event; use "click" instead of "onclick".
Example:

element.addEventListener("click", myFunction);
element.addEventListener("mouseover", myFunction);

function myFunction() {
  alert("Hello World!");
}

This will add two event listeners to the element.
We can remove one of the listeners:

element.removeEventListener("mouseover", myFunction);

Let‘s create an event handler that removes itself after being executed:

<html>
<body>
  <button id="demo">
   Start
  </button>

  <script>
  var btn = document.getElementById("demo");
  btn.addEventListener("click", myFunction);

  function myFunction() {
   alert(Math.random());
   btn.removeEventListener("click", myFunction);
  }
</script>

</body>
</html>
时间: 2024-10-13 18:35:12

Event Listeners的相关文章

让页面滑动流畅得飞起的新特性:Passive Event Listeners

版权声明:本文由陈志兴原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/153 来源:腾云阁 https://www.qcloud.com/community 在不久前的Google I/O 2016 Mobile Web Talk中,Google公布了一个让页面滑动更流畅的新特性Passive Event Listeners.该特性目前已经集成到Chrome51版本中.Chrome51上使用Passive Event L

Ehcache(2.9.x) - API Developer Guide, Cache Event Listeners

About Cache Event Listeners Cache listeners allow implementers to register callback methods that will be executed when a cache event occurs. Cache listeners implement the CacheEventListener interface. The events include: An Element has been put An El

Ehcache(2.9.x) - API Developer Guide, Cache Manager Event Listeners

About CacheManager Event Listeners CacheManager event listeners allow implementers to register callback methods that will be executed when a CacheManager event occurs. CacheManager listeners implement the CacheManagerEventListener interface. The even

java event listeners and dispatcher

reference to blog.csdn.net/5iasp/article/details/37054171 一.场景假设 假设有博客系统中需要实现如下功能: 系统中用户发布文章,修改文章,删除文章时,需要一些相关的操作需要执行. 发布文章后,给好友发送邮件通知,给用户加积分,对文章做全文索引.修改文章后,给好友发送邮件修改通知,给用户加积分,对文章重新做全文索引.删除文章后,给好友发送邮件修改通知,给用户减少积分,对文章重新做全文索引. 二.相关的概念解析 分析如上场景,可以采用事件分发

Cocos2d-X3.0 刨根问底(七)----- 事件机制Event源码分析

这一章,我们来分析Cocos2d-x 事件机制相关的源码, 根据Cocos2d-x的工程目录,我们可以找到所有关于事件的源码都存在放在下图所示的目录中. 从这个event_dispatcher目录中的文件命名上分析 cocos2d-x与事件相关的类一共有四种, Event, EventListener,EventDispatcher, Touch分别为 事件,事件侦听器,事件分发器,触摸 我们先从Event类开始. 打开CCEvent.h文件 /** * Base class of all ki

nova event机制分析

本文主要分析Nova的一个event机制,目前主要用于VIF plugin是的notification,可以实现Nova 和 Neutron直接VIF 状态信息的交互. 1. nova部分 vif_plugging_timeout配置参数的解释,用于定义创建VM时等待VIF准备好的时间 cfg.BoolOpt('vif_plugging_is_fatal', default=True, help="Fail instance boot if vif plugging fails"),

The .NET weak event pattern in C#

Introduction As you may know event handlers are a common source of memory leaks caused by the persistence of objects that are not used anymore, and you may think should have been collected, but are not, and for good reason. … Continue reading → Intro

[DOM Event Learning] Section 4 事件分发和DOM事件流

[DOM Event Learning] Section 4 事件分发和DOM事件流 事件分发机制: event dispatch mechanism. 事件流(event flow)描述了事件对象在数据结构中是如何传播的. 传播路径 事件对象(event objects)被分发给事件目标(event target),在分发开始的时候,在实现中必须先确定事件对象的传播路径. 这个传播路径必须是一个有序的list,其中包含了事件对象必须通过的事件目标. 对于DOM的实现来说,这个传播路径必须反映这

Spring 4.2 annotation event Publisher/Listener

http://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2 Better application events in Spring Framework 4.2 ENGINEERING  STÉPHANE NICOLL FEBRUARY 11, 2015 10 COMMENTS Application events are available since the very beginning