getElementById无法获得图片的left和top值
<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><script type="text/javascript"> function show(){ var img=document.getElementById('pic'); window.alert("left="+img.style.left+" top="+img.style.left); }</script><style type="text/css">.box{ width:500px; height:300px; border:1px solid blue; background-color:#B4CDCD; }#pic{ border:1px solid red; position:relative;}</style></head><body onload="show()"> <div class="box"><img id="pic" alt="图片"/></div></body></html>window.alert("left="+img.offsetLeft+"px top="+img.offsetTop + "px");/*如果没有给 HTML 元素指定过 left样式,则 style.left返回的是空字符串offsetLeft返回当前元素的左边界到它的包含元素的左边界的偏移量,以像素为单位。类型:int。状态:只读。*/
[解决办法]
你应该去了解
currentStyle, runtimeStyle and computedStyle
这3个对象的作用
参加
http://www.code-design.cn/article/20100909/javascript-get-value-from-css-stylesheet.aspx
http://www.strictly-software.com/CSSStyleObject
https://developer.mozilla.org/en/DOM/window.getComputedStyle