zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    bzoj3786 星系探索
    [JSOI2008]火星人
    [NOI2005]维护数列
    [POI2008]砖块Klo
    郁闷的出纳员
    [HNOI2002]营业额统计
    [BZOJ1651][Usaco2006 Feb]Stall Reservations 专用牛棚
    [BZOJ2124]等差子序列
    [BZOJ3038]上帝造题的七分钟2
    [BZOJ1711][Usaco2007 Open]Dining吃饭
  • 原文地址:https://www.cnblogs.com/guojunru/p/5403779.html
Copyright © 2011-2022 走看看