简体   繁体   中英

Trigger (dispatchEvent) the 'onerror' with javascript?

In my code I did: element.onerror = my_function;

I am wondering how to forcefully trigger this event so I can test it?

Can someone show me how to use the dispatchEvent flow to do this?

您可以使用element.dispatchEvent

OKAY I figured it out!

var evt = document.createEvent('HTMLEvents');
evt.initEvent('error', false, false);
element.dispatchEvent(evt);

I figured out that onerror is an 'HTMLEvents' according to google. Since the event name is error , the event is error , and I knew that error doesn't bubble, hence the false in the second parameter of initEvent .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM