这段代码为什么缺少对象



www.yuanshi88.com alert(document.getElementById("a1").style.width);



网页错误详细信息

用户代理: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; .NET4.0C)
时间戳: Mon, 29 Aug 2011 13:35:41 UTC

消息: 缺少对象
行: 4
字符: 1
代码: 0
URI: file:///C:/Users/denggg/Desktop/2.html

页面是顺序加载的,改成[code="js"]


www.yuanshi88.com

alert(document.getElementById("a1").style.width);


[/code]
或者[code="js"]


www.yuanshi88.com window.onload = function(){ alert(document.getElementById("a1").style.width); }



[/code]

页面装载顺序问题
执行alert(document.getElementById("a1").style.width);
时,

还没有初始化,所以document.getElementById("a1")为null

[code="html"]


www.yuanshi88.com function test(){ alert(document.getElementById("a1").style.width); }



[/code]

顺序错误 要知道页面编译时时自上而下的 把script放到下面就行啦

编译时从上而下的,运行到js时候,a1还没加载所以会找不到对象。
一般企业开发js全部是放在最后一个