【讀書筆記】 經過原生javascript獲取margin

 

The properties on the style object are only the styles applied directly to the element (e.g., via a style attribute or in code). So .style.marginTop will only have something in it if you have something specifically assigned to that element (not assigned via a style sheet, etc.).app

To get the current calculated style of the object, you use either the currentStyle property (Microsoft) or the getComputedStyle function (pretty much everyone else).spa

Example:code

var p = document.getElementById("target"); var style = p.currentStyle || window.getComputedStyle(p); display("Current marginTop: " + style.marginTop);

Fair warning: What you get back may not be in pixels. For instance, if I run the above on a pelement in IE9, I get back "1em".ci

相關文章
相關標籤/搜索