1
window.addEventListener("resize", getSize);
function getSize(){
var node = document.getElementById('sample');
var height = node.style.height;
console.log('height: ', height);//expect 100px, get nothing
}
#sample {
padding: 20px;
height: 100px;
overflow-y: auto;
border: solid blue;
margin: 10px 300px 10px 25px;
position: 'relative'
}
node.style.height
的高度總是空/空。請注意,我將高度設置爲100px。爲什麼?我怎樣才能獲得節點的CSS高度?
使用'節點。 offsetHeight'。 –
或node.clientHeight – n0m4d
請注意,您仍然需要使用'node.style.height ='1000px';'來設置值。設置'offsetHeight'不起作用。 –