Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 如何在 JavaScript JavaScript 自定义事件监听器中触发事件
我以这种方式创建了新的自定义事件:
window.addEventListener('customEvent', function () { doSomethingOnEvent(); }, false);
如何触发这种自定义事件?
您必须先初始化事件:
var myCustomEvent = document.createEvent("Event"); myCustomEvent.initEvent("customEvent",true,true);
然后触发它:
document.dispatchEvent(myCustomEvent);