如果在元素加载后设置了onload属性,应该会发生什么?处理程序是立即执行还是被忽略?
答案 0 :(得分:2)
无。它被忽略了。事件发生时,事件处理程序不会被激活。
答案 1 :(得分:0)
你能不能给出一个你要问的例子,我认为这段代码可以帮助你:
function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
<script type="text/javascript" src="http://your.cdn.com/first.js"></script>
<script type="text/javascript">
loadScript("http://your.cdn.com/second.js", function(){
//initialization code
});
</script>