问下script放在body里面和放在下面的区别?
如果要是像老师那样把script放在下面的话,程序顺利执行。
但是想把script放在head里面用window.onload的话,程序就报错说ScrollUp没有定义,如下面的代码:
window.onload=function(){ var area = document.getElementById('moocBox'), con1 = document.getElementById('con1'), con2 = document.getElementById('con2'); var time = 10; con2.innerHTML = con1.innerHTML; var timer = null; area.onmouseover=function(){ timer=setInterval('scrollUp()',time); } area.onmouseout=function(){ clearInterval(timer); } function scrollUp(){ if (area.scrollTop >= con1.offsetHeight) { area.scrollTop = 0; } else { area.scrollTop++; } } }
但是如果把scrollUp写在window.onload外面就可以了,像下面这样:
window.onload = function() { var area = document.getElementById('moocBox'), con1 = document.getElementById('con1'), con2 = document.getElementById('con2'); var time = 10; con2.innerHTML = con1.innerHTML; var timer = null; area.onmouseover = function() { timer = setInterval('scrollUp()', time); } area.onmouseout = function() { clearInterval(timer); } } function scrollUp() { var area = document.getElementById('moocBox'), con1 = document.getElementById('con1'), con2 = document.getElementById('con2'); if (area.scrollTop >= con1.offsetHeight) { area.scrollTop = 0; } else { area.scrollTop++; } }
这是为什么?