Site icon 时鹏亮的Blog

如何实现JavaScript触发事件

请知悉:本文最近一次更新为 1 year 前,文中内容可能已经过时。

有的时候需要触发一些指定事件,比如mouseover,那么JavaScript如何触发事件呢?

示例代码如下:

const event = document.createEvent('Events');
event.initEvent( 'click', true, false );
const targetElement = document.getElementById('someID');
targetElement.dispatchEvent(event);

参考资料

如何实现JS主动触发事件


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《如何实现JavaScript触发事件》相关的博文:

Exit mobile version