今天在用原生js獲取div的margin時候遇到了困難,然后記錄下來,以免下次解決不了類似問題。
var box = document.querySelector("div");
console.log(getStyle(box, "marginTop"));
function getStyle(ele, attr) {
if (window.getComputedStyle) {
return window.getComputedStyle(ele, null)[attr];
} else {
return ele.currentStyle[attr];
}
}