相关疑难解决方法(0)

不使用jQuery触发自定义事件

我用jQuery触发了一些DOM事件 triggerHandler()

<!DOCTYPE html>
<html>
<head>
  <title>stackoverflow</title>
  <script src="http://ajax.googleapis.com:80/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>
  <script>
    $(document).ready(function() {
      $(document).on('hey', function(customEvent, originalEvent, data) {
        console.log(customEvent.type + ' ' + data.user); // hey stackoverflow

      });

      // how to this in vanilla js
      $(document).triggerHandler('hey', [{}, {
        'user': 'stackoverflow'
      }])
    });
  </script>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

如何在没有jQuery的情况下触发这个?

重要提示:我需要知道事件类型和自定义数据

javascript jquery dom-events jquery-triggerhandler

16
推荐指数
1
解决办法
5148
查看次数