Trying to fire the onload event on script tag
我正试图按顺序加载一组脚本,但OnLoad事件不会为我触发。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | var scripts = [ '//cdnjs.cloudflare.com/ajax/libs/less.js/1.3.3/less.min.js', '//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.min.js', MK.host+'/templates/templates.js' ]; function loadScripts(scripts){ var script = scripts.shift(); var el = document.createElement('script'); el.src = script; el.onload = function(script){ console.log(script + ' loaded!'); if (scripts.length) { loadScripts(scripts); } else { console.log('run app'); MK.init(); } }; $body.append(el); } loadScripts(scripts); |
我猜想,当jQuery被用来将元素追加到DOM时,像E.OnLoad这样的本地事件不会发生火灾。如果我使用本机
您应该在
1 2 | el.onload = function() { //... el.src = script; |
在附加
1 2 3 | $body.append(el); el.onload = function() { //... el.src = script; |
记住,您需要检查