ready和DOMContentLoaded顺序问题
jq的ready事件底层是调用了DOMContentLoaded,那为什么是ready先被触发,DOMContentLoaded后触发,最后顺序是,“脚本解析二”,“DOMContentLoaded回调”
jq的ready事件底层是调用了DOMContentLoaded,那为什么是ready先被触发,DOMContentLoaded后触发,最后顺序是,“脚本解析二”,“DOMContentLoaded回调”
2016-01-14
改成这样来看
<script>
window.addEventListener("load", function() {
show('load事件回调')
}, false);
document.addEventListener("DOMContentLoaded", function() {
show('DOMContentLoaded回调')
}, false);
</script>
<script src="http://img1.sycdn.imooc.com//down/540812440001e40e00000000.js" type="text/javascript"></script>
<script>
show('观察脚本加载的顺序')
show('脚本解析一')
//测试加载
$(function(){
show('脚本解析二')
})
show('脚本解析三')
</script>
(function () { var succsee = function () { ready.fun() }; function ready(fun) { if(ready.fun){ var ofun = ready.fun; ready.fun = function () { ofun(); fun(); } console.log(ready.fun) }else{ ready.fun = fun; } } document.addEventListener('DOMContentLoaded',succsee,false) window.ready = ready; })()
简单的猜测了一下$.ready事件,在加载JQuery时就给document的DOMContentLoaded事件绑定complete(success)函数,函数complete(success)再执行你想执行的函数,而不是直接给document的DOMContentLoaded事件绑定你想要的函数
举报