自定义事件

记录贴

方式有几种吧,个人习惯这种写法

<button class="btn">自定义</button>

let btn = document.querySelector(".btn")
const myClick = new Event("ccx")
btn.addEventListener("ccx",function(){
    console.log("执行了")
})
     
btn.onclick=function(){
    console.log("点击")
    btn.dispatchEvent(myClick)
}
原文地址:https://www.cnblogs.com/styleFeng/p/14193583.html