0
我想檢索身體的scrollHeight爲了重置ID =「background1」的div的大小。如果在我的外部CSS中,我將該ID設置爲position:absolute,scrollHeight總是返回0.如果將其設置爲position:relative scrollHeight給了我正確的值。當然,CSS中有更多的選擇器,其中一個被設置爲絕對位置:保持原樣不會改變上述行爲。代碼:scrollHeight不與位置:絕對
<script type="text/javascript">
function getElement(elem) {
var art;
if (document.all) { // this is the way old msie versions work
art = document.all[elem];
}
else if (document.layers) { // this is the way nn4 works
art = document.layers[elem];
}
else if (document.getElementById) { // this is the way the standards work
art = document.getElementById(elem);
}
return art;
}
function toggleArticleDisplay(elem) {
var artArray = document.getElementsByTagName("article");
for(var i = 0; i < artArray.length; i++) {
artArray[i].style.display = "none";
}
var art = getElement(elem);
art.style.display = "block";
var bg1 = document.getElementById("background1");
bg1.style.height = "550px";
var b = document.getElementById("potoococha");
alert(b.scrollHeight);
bg1.style.height = (b.scrollHeight >= window.innerHeight) ?
(b.scrollHeight+20) + "px" : window.innerHeight + "px";
}
</script>
</head>
<body id="potoococha" onLoad="toggleArticleDisplay('homeText')">
<div id="background1" style="height:615px;"/>
而現在的CSS:
div#background1 { position : absolute;
left : 45px;
top : 0px;
z-index : 1;
width : 50px;
background-color : rgba(182,181,91,1.0); }
僅僅改變那個位置相對使javascript函數返回警報適當scrollHeight屬性,但我需要這個因爲其他原因而絕對的。如果某些元素被絕對定位,scrollHeight是否不起作用?正如我所說,還有另一個絕對定位的元素,它對scrollHeight的返回值沒有任何影響。只有身體元素的第一個孩子纔是這個元素似乎是一個問題。有任何想法嗎?