addEventListener的第二个参数为对象

  1. 改变this的指向
  2. 将对象的handleEvent方法作为事件回调。
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="wrap">
      handle-event
    </div>
    <script>
      const div = document.querySelector("#wrap");
      const obj = {
        name: "huang",
        handleEvent: function() {
          console.log(this.name);
        }
      };
      div.addEventListener("click", obj);
    </script>
  </body>
</html>

posted @ 2020-04-02 14:48  菜鸡_chicken  阅读(421)  评论(0编辑  收藏  举报