javascript 中 clientHeight() 和 offsetHeight() 有什么区别?
javascriptfront end technologyobject oriented programming
您可以使用 2 个属性 clientHeight 和 offsetHeight 来获取 div 的高度。
clientHeight 包括 div 的填充。
offsetHeight 包括 div 的填充、滚动条和边框。
示例
例如,如果您有以下 HTML −
<div id="myDiv" height="400px"></div>
您可以使用以下方法获取高度 −
const height = document.querySelector('#myDiv').offsetHeight console.log(height)
输出
这将给出输出 −
400