Felix KlingEdza提出了一个问题:Full height of a html element (div) including border, padding and margin?,或许与您遇到的问题类似。
回答者Vitim.usgdoron给出了该问题的处理方式:
If you can use jQuery:
$('#divId').outerHeight(true) // gives with margins.
For vanilla javascript you need to write a lot more (like always...):
function Dimension(elmID) {
var elmHeight, elmMargin, elm = document.getElementById(elmID);
if(document.all) {// IE
elmHeight = elm.currentStyle.height;
elmMargin = parseInt(elm.currentStyle.marginTop, 10) + parseInt(elm.currentStyle.marginBottom, 10) + "px";
} else {// Mozilla
elmHeight = document.defaultView.getComputedStyle(elm, '').getPropertyValue('height');
elmMargin = parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-top')) + parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-bottom')) + "px";
}
return (elmHeight+elmMargin);
}
希望本文对你有帮助,欢迎支持JavaScript中文网