本文介绍了手动dispatchEvent DOMContentLoaded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法手动触发DOMContentLoaded事件?我试图为某些客户端JavaScript编写一个单元测试,它在DOMContentLoaded事件中做了一些事情。以下内容不起作用:document.dispatchEvent DOMContentLoaded)或document.body.dispatchEvent(DOMContentLoaded);
解决方案
这适用于Firefox: p>
var DOMContentLoaded_event = document.createEvent(Event)
DOMContentLoaded_event.initEvent(DOMContentLoaded,true,true)
window.document.dispatchEvent(DOMContentLoaded_event)
Is there any way to manually fire the DOMContentLoaded event? I'm trying to write a unit-test for some client-side javascript which does some stuff on the DOMContentLoaded event.
The following did not work: document.dispatchEvent("DOMContentLoaded") or document.body.dispatchEvent("DOMContentLoaded");
解决方案
This works for me in Firefox:
var DOMContentLoaded_event = document.createEvent("Event")
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true)
window.document.dispatchEvent(DOMContentLoaded_event)
这篇关于手动dispatchEvent DOMContentLoaded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!