// 获得元素样式function attrStyle(elem,attr){ if("undefined" != typeof elem && "undefined" != typeof elem.style){ //若样式存在于html中,优先获取 return elem.style[attr]; }else if(elem.currentStyle){ //IE下获取CSS属性最终样式(同于CSS优先级) return elem.currentStyle[attr]; }else if(document.defaultView && document.defaultView.getComputedStyle){ //W3C标准方法获取CSS属性最终样式(同于CSS优先级) //注意,此法属性原格式(text-align)获取的,故要转换一下 attr=attr.replace(/([A-Z])/g,'-$1').toLowerCase(); //获取样式对象并获取属性值 return document.defaultView.getComputedStyle(elem,null).getPropertyValue(attr); }else{ return null; }}attrStyle(document.getElementById("coorDiv"), "margin-left");
// 获得元素位置
function elementLeft(e){ var offset = e.offsetLeft; if(e.offsetParent != null) offset += elementLeft(e.offsetParent); return offset;}function elementTop(e){ var offset=e.offsetTop; if(e.offsetParent != null) offset += elementTop(e.offsetParent); return offset;}function elementPos(e){ return {left:elementLeft(e), top:elementTop(e)};}