1. 创建事件: document.createEvent();

2. 触发事件: document.dispatchEvent();

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="butt">Click</button>
    <script>
        // 创建一个Event实例对象
        var event = document.createEvent('Event');

        // 初始化事件信息
        event.initEvent('build', true, true);

        // 添加事件监听函数
        document.addEventListener('build', function (e) {
            console.log(e.type); // "build"
        }, false);

        // 触发事件
        document.dispatchEvent(event);
    </script>
</body>

</html>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
  • 2021-06-18
  • 2021-12-05
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-13
  • 2021-11-06
  • 2021-05-16
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
相关资源
相似解决方案